home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / sed 2.0.3 / rx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-09  |  216.9 KB  |  4,417 lines  |  [TEXT/MPS ]

  1. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17. /* NOTE!!!  AIX requires this to be the first thing in the file.
  18.  * Do not put ANYTHING before it!  
  19.  */
  20. #if !defined (__GNUC__) && defined (_AIX)
  21.  #pragma alloca
  22. #endif
  23.  
  24. static char rx_version_string[] = "GNU Rx version 0.03";
  25.  
  26.             /* ``Too hard!''
  27.              *        -- anon.
  28.              */
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #ifndef isgraph
  33. #define isgraph(c) (isprint (c) && !isspace (c))
  34. #endif
  35. #ifndef isblank
  36. #define isblank(c) ((c) == ' ' || (c) == '\t')
  37. #endif
  38.  
  39. #include <sys/types.h>
  40. #include <stdio.h>
  41. #include "rx.h"
  42.  
  43. #undef MAX
  44. #undef MIN
  45. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  46. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  47.  
  48. typedef char boolean;
  49. #define false 0
  50. #define true 1
  51.  
  52.  
  53. /* This page is decls to the interesting subsystems and lower layers
  54.  * of rx.  Everything which doesn't have a public counterpart in 
  55.  * regex.c is declared here.
  56.  * 
  57.  * A useful (i hope) system is obtained by removing all or part of the regex.c
  58.  * reimplementation and making these all extern.  I think this package
  59.  * could be used to implement on-line lexers and parsers and who knows what 
  60.  * else.
  61.  */
  62. /* In the definitions, these functions are qualified by `RX_DECL' */
  63. #define RX_DECL static
  64.  
  65. #ifdef __STDC__
  66.  
  67. RX_DECL int rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b);
  68. RX_DECL void rx_bitset_null (int size, rx_Bitset b);
  69. RX_DECL void rx_bitset_universe (int size, rx_Bitset b);
  70. RX_DECL void rx_bitset_complement (int size, rx_Bitset b);
  71. RX_DECL void rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b);
  72. RX_DECL void rx_bitset_union (int size, rx_Bitset a, rx_Bitset b);
  73. RX_DECL void rx_bitset_intersection (int size,
  74.                      rx_Bitset a, rx_Bitset b);
  75. RX_DECL void rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b);
  76. RX_DECL unsigned long rx_bitset_hash (int size, rx_Bitset b);
  77. RX_DECL struct rx_hash_item * rx_hash_find (struct rx_hash * table,
  78.                         unsigned long hash,
  79.                         void * value,
  80.                         struct rx_hash_rules * rules);
  81. RX_DECL struct rx_hash_item * rx_hash_store (struct rx_hash * table,
  82.                          unsigned long hash,
  83.                          void * value,
  84.                          struct rx_hash_rules * rules);
  85. RX_DECL void rx_hash_free (struct rx_hash_item * it,
  86.                struct rx_hash_rules * rules);
  87. RX_DECL rx_Bitset rx_cset (struct rx *rx);
  88. RX_DECL rx_Bitset rx_copy_cset (struct rx *rx, rx_Bitset a);
  89. RX_DECL void rx_free_cset (struct rx * rx, rx_Bitset c);
  90. RX_DECL struct rexp_node * rexp_node (struct rx *rx,
  91.                       enum rexp_node_type type);
  92. RX_DECL struct rexp_node * rx_mk_r_cset (struct rx * rx,
  93.                      rx_Bitset b);
  94. RX_DECL struct rexp_node * rx_mk_r_concat (struct rx * rx,
  95.                        struct rexp_node * a,
  96.                        struct rexp_node * b);
  97. RX_DECL struct rexp_node * rx_mk_r_alternate (struct rx * rx,
  98.                           struct rexp_node * a,
  99.                           struct rexp_node * b);
  100. RX_DECL struct rexp_node * rx_mk_r_opt (struct rx * rx,
  101.                     struct rexp_node * a);
  102. RX_DECL struct rexp_node * rx_mk_r_star (struct rx * rx,
  103.                      struct rexp_node * a);
  104. RX_DECL struct rexp_node * rx_mk_r_2phase_star (struct rx * rx,
  105.                         struct rexp_node * a,
  106.                         struct rexp_node * b);
  107. RX_DECL struct rexp_node * rx_mk_r_side_effect (struct rx * rx,
  108.                         rx_side_effect a);
  109. RX_DECL struct rexp_node * rx_mk_r_data  (struct rx * rx,
  110.                       void * a);
  111. RX_DECL void rx_free_rexp (struct rx * rx, struct rexp_node * node);
  112. RX_DECL struct rexp_node * rx_copy_rexp (struct rx *rx,
  113.                      struct rexp_node *node);
  114. RX_DECL struct rx_nfa_state * rx_nfa_state (struct rx *rx);
  115. RX_DECL void rx_free_nfa_state (struct rx_nfa_state * n);
  116. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state (struct rx * rx,
  117.                           int id);
  118. RX_DECL struct rx_nfa_edge * rx_nfa_edge (struct rx *rx,
  119.                       enum rx_nfa_etype type,
  120.                       struct rx_nfa_state *start,
  121.                       struct rx_nfa_state *dest);
  122. RX_DECL void rx_free_nfa_edge (struct rx_nfa_edge * e);
  123. RX_DECL void rx_free_nfa (struct rx *rx);
  124. RX_DECL int rx_build_nfa (stru }
  125.  
  126.  
  127.     case r_concat:
  128.       {
  129.     struct rx_nfa_state *shared = 0;
  130.     return
  131.       (rx_build_nfa (rx, rexp->params.pair.left, start, &shared)
  132.        && rx_build_nfa (rx, rexp->params.pair.right, &shared, end));
  133.       }
  134.  
  135.     case r_alternate:
  136.       {
  137.     struct rx_nfa_state *ls = 0;
  138.     struct rx_nfa_state *le = 0;
  139.     struct rx_nfa_state *rs = 0;
  140.     struct rx_nfa_state *re = 0;
  141.     return (rx_build_nfa (rx, rexp->params.pair.left, &ls, &le)
  142.         && rx_build_nfa (rx, rexp->params.pair.right, &rs, &re)
  143.         && rx_nfa_edge (rx, ne_epsilon, *start, ls)
  144.         && rx_nfa_edge (rx, ne_epsilon, *start, rs)
  145.         && rx_nfa_edge (rx, ne_epsilon, le, *end)
  146.         && rx_nfa_edge (rx, ne_epsilon, re, *end));
  147.       }
  148.  
  149.     case r_side_effect:
  150.       edge = rx_nfa_edge (rx, ne_side_effect, *start, *end);
  151.       if (!edge)
  152.     return 0;
  153.       edge->params.side_effect = rexp->params.side_effect;
  154.       return 1;
  155.     };
  156. }
  157.  
  158.  
  159. /* NAME_RX->NFA_STATES identifies all nodes with non-epsilon transitions.
  160.  * These nodes can occur in super-states.  All nodes are given an integer id.
  161.  * The id is non-negative if the node has non-epsilon out-transitions, negative
  162.  * otherwise (this is because we want the non-negative ids to be used as 
  163.  * array indexes in a few places).
  164.  */
  165.  
  166. #ifdef __STDC__
  167. RX_DECL void
  168. rx_name_nfa_states (struct rx *rx)
  169. #else
  170. RX_DECL void
  171. rx_name_nfa_states (rx)
  172.      struct rx *rx;
  173. #endif
  174. {
  175.   struct rx_nfa_state *n = rx->nfa_states;
  176.  
  177.   rx->nodec = 0;
  178.   rx->epsnodec = -1;
  179.  
  180.   while (n)
  181.     {
  182.       struct rx_nfa_edge *e = n->edges;
  183.  
  184.       if (n->is_start)
  185.     n->eclosure_needed = 1;
  186.  
  187.       while (e)
  188.     {
  189.       switch (e->type)
  190.         {
  191.         case ne_epsilon:
  192.         case ne_side_effect:
  193.           break;
  194.  
  195.         case ne_cset:
  196.           n->id = rx->nodec++;
  197.           {
  198.         struct rx_nfa_edge *from_n = n->edges;
  199.         while (from_n)
  200.           {
  201.             from_n->dest->eclosure_needed = 1;
  202.             from_n = from_n->next;
  203.           }
  204.           }
  205.           goto cont;
  206.         }
  207.       e = e->next;
  208.     }
  209.       n->id = rx->epsnodec--;
  210.     cont:
  211.       n = n->next;
  212.     }
  213.   rx->epsnodec = -rx->epsnodec;
  214. }
  215.  
  216.  
  217. /* This page: data structures for the static part of the nfa->supernfa
  218.  * translation.
  219.  */
  220.  
  221. /* The next several functions compare, construct, etc. lists of side
  222.  * effects.  See ECLOSE_NFA (below) for details.
  223.  */
  224.  
  225. /* Ordering of rx_se_list
  226.  * (-1, 0, 1 return value convention).
  227.  */
  228.  
  229. #ifdef __STDC__
  230. static int 
  231. se_list_cmp (void * va, void * vb)
  232. #else
  233. static int 
  234. se_list_cmp (va, vb)
  235.      void * va;
  236.      void * vb;
  237. #endif
  238. {
  239.   struct rx_se_list * a = (struct rx_se_list *)va;
  240.   struct rx_se_list * b = (struct rx_se_list *)vb;
  241.  
  242.   return ((va == vb)
  243.       ? 0
  244.       : (!va
  245.          ? -1
  246.          : (!vb
  247.         ? 1
  248.         : ((long)a->car < (long)b->car
  249.            ? 1
  250.            : ((long)a->car > (long)b->car
  251.               ? -1
  252.               : se_list_cmp ((void *)a->cdr, (void *)b->cdr))))));
  253. }
  254.  
  255.  
  256. #ifdef __STDC__
  257. static int 
  258. se_list_equal (void * va, void * vb)
  259. #else
  260. static int 
  261. se_list_equal (va, vb)
  262.      void * va;
  263.      void * vb;
  264. #endif
  265. {
  266.   return !(se_list_cmp (va, vb));
  267. }
  268.  
  269. static struct rx_hash_rules se_list_hash_rules =
  270. {
  271.   se_list_equal,
  272.   compiler_hash_alloc,
  273.   compiler_free_hash,
  274.   compiler_hash_item_alloc,
  275.   compiler_free_hash_item
  276. };
  277.  
  278. static DEF_POOL(sel_pool, struct rx_se_list);
  279.  
  280. #ifdef __STDC__
  281. static struct rx_se_list * 
  282. side_effect_cons (struct rx * rx,
  283.           void * se, struct rx_se_list * list)
  284. #else
  285. static struct rx_se_list * 
  286. side_effect_cons (rx, se, list)
  287.      struct rx * rx;
  288.      void * se;
  289.      struct rx_se_list * list;
  290. #endif
  291. {
  292.   struct rx_se_list * l = ((struct rx_se_list *)
  293.                chunk_malloc (&sel_pool));
  294.   if (!l)
  295.     return 0;
  296.   l->car = se;
  297.   l->cdr = list;
  298.   return l;
  299. }
  300.  
  301.  
  302. #ifdef __STDC__
  303. static struct rx_se_list *
  304. hash_cons_se_prog (struct rx * rx,
  305.            struct rx_hash * memo,
  306.            void * car, struct rx_se_list * cdr)
  307. #else
  308. static struct rx_se_list *
  309. hash_cons_se_prog (rx, memo, car, cdr)
  310.      struct rx * rx;
  311.      struct rx_hash * memo;
  312.      void * car;
  313.      struct rx_se_list * cdr;
  314. #endif
  315. {
  316.   long hash = (long)car ^ (long)cdr;
  317.   struct rx_se_list template;
  318.  
  319.   template.car = car;
  320.   template.cdr = cdr;
  321.   {
  322.     struct rx_hash_item * it = rx_hash_store (memo, hash,
  323.                           (void *)&template,
  324.                           &se_list_hash_rules);
  325.     if (!it)
  326.       return 0;
  327.     if (it->data == (void *)&template)
  328.       {
  329.     struct rx_se_list * consed = ((struct rx_se_list *)
  330.                       chunk_malloc (&sel_pool));
  331.     *consed = template;
  332.     it->data = (void *)consed;
  333.       }
  334.     return (struct rx_se_list *)it->data;
  335.   }
  336. }
  337.      
  338.  
  339. #ifdef __STDC__
  340. static struct rx_se_list *
  341. hash_se_prog (struct rx * rx, struct rx_hash * memo, struct rx_se_list * prog)
  342. #else
  343. static struct rx_se_list *
  344. hash_se_prog (rx, memo, prog)
  345.      struct rx * rx;
  346.      struct rx_hash * memo;
  347.      struct rx_se_list * prog;
  348. #endif
  349. {
  350.   struct rx_se_list * answer = 0;
  351.   while (prog)
  352.     {
  353.       answer = hash_cons_se_prog (rx, memo, prog->car, answer);
  354.       if (!answer)
  355.     return 0;
  356.       prog = prog->cdr;
  357.     }
  358.   return answer;
  359. }
  360.  
  361.  
  362. /* This page: more data structures for nfa->supernfa.  Specificly,
  363.  * sets of nfa states.
  364.  */
  365.  
  366. #ifdef __STDC__
  367. static int 
  368. nfa_set_cmp (void * va, void * vb)
  369. #else
  370. static int 
  371. nfa_set_cmp (va, vb)
  372.      void * va;
  373.      void * vb;
  374. #endif
  375. {
  376.   struct rx_nfa_state_set * a = (struct rx_nfa_state_set *)va;
  377.   struct rx_nfa_state_set * b = (struct rx_nfa_state_set *)vb;
  378.  
  379.   return ((va == vb)
  380.       ? 0
  381.       : (!va
  382.          ? -1
  383.          : (!vb
  384.         ? 1
  385.         : (a->car->id < b->car->id
  386.            ? 1
  387.            : (a->car->id > b->car->id
  388.               ? -1
  389.               : nfa_set_cmp ((void *)a->cdr, (void *)b->cdr))))));
  390. }
  391.  
  392. #ifdef __STDC__
  393. static int 
  394. nfa_set_equal (void * va, void * vb)
  395. #else
  396. static int 
  397. nfa_set_equal (va, vb)
  398.      void * va;
  399.      void * vb;
  400. #endif
  401. {
  402.   return !nfa_set_cmp (va, vb);
  403. }
  404.  
  405. static struct rx_hash_rules nfa_set_hash_rules =
  406. {
  407.   nfa_set_equal,
  408.   compiler_hash_alloc,
  409.   compiler_free_hash,
  410.   compiler_hash_item_alloc,
  411.   compiler_free_hash_item
  412. };
  413.  
  414.  
  415. /* CONS -- again, sets with == elements are ==. */
  416.  
  417. static DEF_POOL(nfa_sets, struct rx_nfa_state_set);
  418.  
  419. #ifdef __STDC__
  420. static struct rx_nfa_state_set * 
  421. nfa_set_cons (struct rx * rx,
  422.           struct rx_hash * memo, struct rx_nfa_state * state,
  423.           struct rx_nfa_state_set * set)
  424. #else
  425. static struct rx_nfa_state_set * 
  426. nfa_set_cons (rx, memo, state, set)
  427.      struct rx * rx;
  428.      struct rx_hash * memo;
  429.      struct rx_nfa_state * state;
  430.      struct rx_nfa_state_set * set;
  431. #endif
  432. {
  433.   struct rx_nfa_state_set template;
  434.   struct rx_hash_item * node;
  435.   template.car = state;
  436.   template.cdr = set;
  437.   node = rx_hash_store (memo,
  438.             (((long)state) >> 8) ^ (long)set,
  439.             &template, &nfa_set_hash_rules);
  440.   if (!node)
  441.     return 0;
  442.   if (node->data == &template)
  443.     {
  444.       struct rx_nfa_state_set * l = ((struct rx_nfa_state_set *)
  445.                   chunk_malloc (&nfa_sets));
  446.       node->data = (void *) l;
  447.       if (!l)
  448.     return 0;
  449.       *l = template;
  450.     }
  451.   return (struct rx_nfa_state_set *)node->data;
  452. }
  453.  
  454.  
  455. #ifdef __STDC__
  456. static struct rx_nfa_state_set * 
  457. nfa_set_enjoin (struct rx * rx,
  458.         struct rx_hash * memo, struct rx_nfa_state * state,
  459.         struct rx_nfa_state_set * set)
  460. #else
  461. static struct rx_nfa_state_set * 
  462. nfa_set_enjoin (rx, memo, state, set)
  463.      struct rx * rx;
  464.      struct rx_hash * memo;
  465.      struct rx_nfa_state * state;
  466.      struct rx_nfa_state_set * set;
  467. #endif
  468. {
  469.   if (!set || state->id < set->car->id)
  470.     return nfa_set_cons (rx, memo, state, set);
  471.   if (state->id == set->car->id)
  472.     return set;
  473.   else
  474.     {
  475.       struct rx_nfa_state_set * newcdr
  476.     = nfa_set_enjoin (rx, memo, state, set->cdr);
  477.       if (newcdr != set->cdr)
  478.     set = nfa_set_cons (rx, memo, set->car, newcdr);
  479.       return set;
  480.     }
  481. }
  482.  
  483.  
  484.  
  485. /* This page: computing epsilon closures.  The closures aren't total.
  486.  * Each node's closures are partitioned according to the side effects entailed
  487.  * along the epsilon edges.  Return true on success.
  488.  */ 
  489.  
  490. struct eclose_frame
  491. {
  492.   struct rx_se_list *prog_backwards;
  493. };
  494.  
  495.  
  496. #ifdef __STDC__
  497. static int 
  498. eclose_node (struct rx *rx, struct rx_nfa_state *outnode,
  499.          struct rx_nfa_state *node, struct eclose_frame *frame)
  500. #else
  501. static int 
  502. eclose_node (rx, outnode, node, frame)
  503.      struct rx *rx;
  504.      struct rx_nfa_state *outnode;
  505.      struct rx_nfa_state *node;
  506.      struct eclose_frame *frame;
  507. #endif
  508. {
  509.   struct rx_nfa_edge *e = node->edges;
  510.  
  511.   /      cache->lru_superstate = cache->lru_superstate->next_recyclable;
  512.       ++locked_superstates;
  513.       if (locked_superstates == cache->superstates)
  514.         return 0;
  515.     }
  516.       it = cache->lru_superstate;
  517.       it->next_recyclable->prev_recyclable = it->prev_recyclable;
  518.       it->prev_recyclable->next_recyclable = it->next_recyclable;
  519.       cache->lru_superstate = ((it == it->next_recyclable)
  520.                     ? 0
  521.                     : it->next_recyclable);
  522.     }
  523.  
  524.   if (it->transition_refs)
  525.     {
  526.       struct rx_distinct_future *df;
  527.       for (df = it->transition_refs,
  528.        df->prev_same_dest->next_same_dest = 0;
  529.        df;
  530.        df = df->next_same_dest)
  531.     {
  532.       df->future_frame.inx = cache->instruction_table[rx_cache_miss];
  533.       df->future_frame.data = 0;
  534.       df->future_frame.data_2 = (void *) df;
  535.       df->future = 0;
  536.     }
  537.       it->transition_refs->prev_same_dest->next_same_dest =
  538.     it->transition_refs;
  539.     }
  540.   {
  541.     struct rx_super_edge *tc = it->edges;
  542.     while (tc)
  543.       {
  544.     struct rx_distinct_future * df;
  545.     struct rx_super_edge *tct = tc->next;
  546.     df = tc->options;
  547.     df->next_same_super_edge[1]->next_same_super_edge[0] = 0;
  548.     while (df)
  549.       {
  550.         struct rx_distinct_future *dft = df;
  551.         df = df->next_same_super_edge[0];
  552.         
  553.         
  554.         if (dft->future && dft->future->transition_refs == dft)
  555.           {
  556.         dft->future->transition_refs = dft->next_same_dest;
  557.         if (dft->future->transition_refs == dft)
  558.           dft->future->transition_refs = 0;
  559.           }
  560.         dft->next_same_dest->prev_same_dest = dft->prev_same_dest;
  561.         dft->prev_same_dest->next_same_dest = dft->next_same_dest;
  562.         rx_cache_free (cache, &cache->free_discernable_futures,
  563.                (char *)dft);
  564.       }
  565.     rx_cache_free (cache, &cache->free_transition_classes, (char *)tc);
  566.     tc = tct;
  567.       }
  568.   }
  569.   
  570.   if (it->contents->superstate == it)
  571.     it->contents->superstate = 0;
  572.   release_superset_low (cache, it->contents);
  573.   rx_cache_free (cache, &cache->free_superstates, (char *)it);
  574.   --cache->superstates;
  575.   return 1;
  576. }
  577.  
  578. #ifdef __STDC__
  579. static char *
  580. rx_cache_get (struct rx_cache * cache,
  581.           struct rx_freelist ** freelist)
  582. #else
  583. static char *
  584. rx_cache_get (cache, freelist)
  585.      struct rx_cache * cache;
  586.      struct rx_freelist ** freelist;
  587. #endif
  588. {
  589.   while (!*freelist && rx_really_free_superstate (cache))
  590.     ;
  591.   if (!*freelist)
  592.     return 0;
  593.   {
  594.     struct rx_freelist * it = *freelist;
  595.     *freelist = it->next;
  596.     return (char *)it;
  597.   }
  598. }
  599.  
  600. #ifdef __STDC__
  601. static char *
  602. rx_cache_malloc_or_get (struct rx_cache * cache,
  603.             struct rx_freelist ** freelist, int bytes)
  604. #else
  605. static char *
  606. rx_cache_malloc_or_get (cache, freelist, bytes)
  607.      struct rx_cache * cache;
  608.      struct rx_freelist ** freelist;
  609.      int bytes;
  610. #endif
  611. {
  612.   if (!*freelist)
  613.     {
  614.       char * answer = rx_cache_malloc (cache, bytes);
  615.       if (answer)
  616.     return answer;
  617.     }
  618.  
  619.   return rx_cache_get (cache, freelist);
  620. }
  621.  
  622. #ifdef __STDC__
  623. static char *
  624. rx_cache_get_superstate (struct rx_cache * cache)
  625. #else
  626. static char *
  627. rx_cache_get_superstate (cache)
  628.       struct rx_cache * cache;
  629. #endif
  630. {
  631.   char * answer;
  632.   int bytes = (   sizeof (struct rx_superstate)
  633.            +  cache->local_cset_size * sizeof (struct rx_inx));
  634.   if (!cache->free_superstates
  635.       && (cache->superstates < cache->superstates_allowed))
  636.     {
  637.       answer = rx_cache_malloc (cache, bytes);
  638.       if (answer)
  639.     {
  640.       ++cache->superstates;
  641.       return answer;
  642.     }
  643.     }
  644.   answer = rx_cache_get (cache, &cache->free_superstates);
  645.   if (!answer)
  646.     {
  647.       answer = rx_cache_malloc (cache, bytes);
  648.       if (answer)
  649.     ++cache->superstates_allowed;
  650.     }
  651.   ++cache->superstates;
  652.   return answer;
  653. }
  654.  
  655.  
  656.  
  657. static int
  658. supersetcmp (va, vb)
  659.      void * va;
  660.      void * vb;
  661. {
  662.   struct rx_superset * a = (struct rx_superset *)va;
  663.   struct rx_superset * b = (struct rx_superset *)vb;
  664.   return (   (a == b)
  665.       || (a && b && (a->car == b->car) && (a->cdr == b->cdr)));
  666. }
  667.  
  668.  
  669. #ifdef __STDC__
  670. static struct rx_hash_item *
  671. superset_allocator (struct rx_hash_rules * rules, void * val)
  672. #else
  673. static struct rx_hash_item *
  674. superset_allocator (rules, val)
  675.      struct rx_hash_rules * rules;
  676.      void * val;
  677. #endif
  678. {
  679.   struct rx_cache * cache
  680.     = ((struct rx_cache *)
  681.        ((char *)rules
  682.     - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules)));
  683.   struct rx_superset * template = (struct rx_superset *)val;
  684.   struct rx_superset * newset
  685.     = ((struct rx_superset *)
  686.        rx_cache_malloc_or_get (cache,
  687.                    &cache->free_supersets,
  688.                    sizeof (*template)));
  689.   newset->refs = 0;
  690.   newset->car = template->car;
  691.   newset->id = template->car->id;
  692.   newset->cdr = template->cdr;
  693.   newset->superstate = 0;
  694.   rx_protect_superset (rx, template->cdr);
  695.   newset->hash_item.data = (void *)newset;
  696.   newset->hash_item.binding = 0;
  697.   return &newset->hash_item;
  698. }
  699.  
  700. #ifdef __STDC__
  701. static struct rx_hash * 
  702. super_hash_allocator (struct rx_hash_rules * rules)
  703. #else
  704. static struct rx_hash * 
  705. super_hash_allocator (rules)
  706.      struct rx_hash_rules * rules;
  707. #endif
  708. {
  709.   struct rx_cache * cache
  710.     = ((struct rx_cache *)
  711.        ((char *)rules
  712.     - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules)));
  713.   return ((struct rx_hash *)
  714.       rx_cache_malloc_or_get (cache,
  715.                   &cache->free_hash, sizeof (struct rx_hash)));
  716. }
  717.  
  718.  
  719. #ifdef __STDC__
  720. static void
  721. super_hash_liberator (struct rx_hash * hash, struct rx_hash_rules * rules)
  722. #else
  723. static void
  724. super_hash_liberator (hash, rules)
  725.      struct rx_hash * hash;
  726.      struct rx_hash_rules * rules;
  727. #endif
  728. {
  729.   struct rx_cache * cache
  730.     = ((struct rx_cache *)
  731.        (char *)rules - (long)(&((struct rx_cache *)0)->superset_hash_rules));
  732.   rx_cache_free (cache, &cache->free_hash, (char *)hash);
  733. }
  734.  
  735. #ifdef __STDC__
  736. static void
  737. superset_hash_item_liberator (struct rx_hash_item * it,
  738.                   struct rx_hash_rules * rules)
  739. #else
  740. static void
  741. superset_hash_item_liberator (it, rules) /* Well, it does ya know. */
  742.      struct rx_hash_item * it;
  743.      struct rx_hash_rules * rules;
  744. #endif
  745. {
  746. }
  747.  
  748. int rx_cache_bound = 128;
  749. static int rx_default_cache_got = 0;
  750.  
  751. #ifdef __STDC__
  752. static int
  753. bytes_for_cache_size (int supers, int cset_size)
  754. #else
  755. static int
  756. bytes_for_cache_size (supers, cset_size)
  757.      int supers;
  758.      int cset_size;
  759. #endif
  760. {
  761.   return (int)
  762.     ((float)supers *
  763.      (  (1.03 * (float) (  rx_sizeof_bitset (cset_size)
  764.              + sizeof (struct rx_super_edge)))
  765.       + (1.80 * (float) sizeof (struct rx_possible_future))
  766.       + (float) (  sizeof (struct rx_superstate)
  767.          + cset_size * sizeof (struct rx_inx))));
  768. }
  769.  
  770. #ifdef __STDC__
  771. static void
  772. rx_morecore (struct rx_cache * cache)
  773. #else
  774. static void
  775. rx_morecore (cache)
  776.      struct rx_cache * cache;
  777. #endif
  778. {
  779.   if (rx_default_cache_got >= rx_cache_bound)
  780.     return;
  781.  
  782.   rx_default_cache_got += 16;
  783.   cache->superstates_allowed = rx_cache_bound;
  784.   {
  785.     struct rx_blocklist ** pos = &cache->memory;
  786.     int size = bytes_for_cache_size (16, cache->local_cset_size);
  787.     while (*pos)
  788.       pos = &(*pos)->next;
  789.     *pos = ((struct rx_blocklist *)
  790.         malloc (size + sizeof (struct rx_blocklist))); 
  791.     if (!*pos)
  792.       return;
  793.  
  794.     (*pos)->next = 0;
  795.     (*pos)->bytes = size;
  796.     cache->memory_pos = *pos;
  797.     cache->memory_addr = (char *)*pos + sizeof (**pos);
  798.     cache->bytes_left = size;
  799.   }
  800. }
  801.  
  802. static struct rx_cache default_cache = 
  803. {
  804.   {
  805.     supersetcmp,
  806.     super_hash_allocator,
  807.     super_hash_liberator,
  808.     superset_allocator,
  809.     superset_hash_item_liberator,
  810.   },
  811.   0,
  812.   0,
  813.   0,
  814.   0,
  815.   rx_morecore,
  816.  
  817.   0,
  818.   0,
  819.   0,
  820.   0,
  821.   0,
  822.  
  823.   0,
  824.   0,
  825.  
  826.   0,
  827.  
  828.   0,
  829.   0,
  830.   0,
  831.   0,
  832.   128,
  833.  
  834.   256,
  835.   rx_id_instruction_table,
  836.  
  837.   {
  838.     0,
  839.     0,
  840.     {0},
  841.     {0},
  842.     {0}
  843.   }
  844. };
  845.  
  846. /* This adds an element to a superstate set.  These sets are lists, such
  847.  * that lists with == elements are ==.  The empty set is returned by
  848.  * superset_cons (rx, 0, 0) and is NOT equivelent to 
  849.  * (struct rx_superset)0.
  850.  */
  851.  
  852. #ifdef __STDC__
  853. RX_DECL struct rx_superset *
  854. rx_superset_cons (struct rx * rx,
  855.           struct rx_nfa_state *car, struct rx_superset *cdr)
  856. #else
  857. RX_DECL struct rx_superset *
  858. rx_superset_cons (rx, car, cdr)
  859.      struct rx * rx;
  860.      struct rx_nfa_state *car;
  861.      struct rx_superset *cdr;
  862. #endif
  863. {
  864.   struct rx_cache * cache = rx->cache;
  865.   if (!car && !cdr)
  866.     {
  867.       if (!cache->empty_superset)
  868.     {
  869.       cache->empty_superset
  870.         = ((struct rx_su_frame.inx = rx->instruction_table[rx_cache_miss];
  871.       dfp->future_frame.data = 0;
  872.       dfp->future_frame.data_2 = (void *) dfp;
  873.       dfp->side_effects_frame.inx
  874.         = rx->instruction_table[rx_do_side_effects];
  875.       dfp->side_effects_frame.data = 0;
  876.       dfp->side_effects_frame.data_2 = (void *) dfp;
  877.       dfp->effects = future->effects;
  878.     }
  879.     }
  880.   return df;
  881. }
  882.  
  883.  
  884.  
  885.  
  886. /* This constructs a new superstate from its state set.  The only 
  887.  * complexity here is memory management.
  888.  */
  889. #ifdef __STDC__
  890. RX_DECL struct rx_superstate *
  891. rx_superstate (struct rx *rx,
  892.            struct rx_superset *set)
  893. #else
  894. RX_DECL struct rx_superstate *
  895. rx_superstate (rx, set)
  896.      struct rx *rx;
  897.      struct rx_superset *set;
  898. #endif
  899. {
  900.   struct rx_cache * cache = rx->cache;
  901.   struct rx_superstate * superstate = 0;
  902.  
  903.   /* Does the superstate already exist in the cache? */
  904.   if (set->superstate)
  905.     {
  906.       if (set->superstate->rx_id != rx->rx_id)
  907.     {
  908.       /* Aha.  It is in the cache, but belongs to a superstate
  909.        * that refers to an NFA that no longer exists.
  910.        * (We know it no longer exists because it was evidently
  911.        *  stored in the same region of memory as the current nfa
  912.        *  yet it has a different id.)
  913.        */
  914.       superstate = set->superstate;
  915.       if (!superstate->is_semifree)
  916.         {
  917.           if (cache->lru_superstate == superstate)
  918.         {
  919.           cache->lru_superstate = superstate->next_recyclable;
  920.           if (cache->lru_superstate == superstate)
  921.             cache->lru_superstate = 0;
  922.         }
  923.           {
  924.         superstate->next_recyclable->prev_recyclable
  925.           = superstate->prev_recyclable;
  926.         superstate->prev_recyclable->next_recyclable
  927.           = superstate->next_recyclable;
  928.         if (!cache->semifree_superstate)
  929.           {
  930.             (cache->semifree_superstate
  931.              = superstate->next_recyclable
  932.              = superstate->prev_recyclable
  933.              = superstate);
  934.           }
  935.         else
  936.           {
  937.             superstate->next_recyclable = cache->semifree_superstate;
  938.             superstate->prev_recyclable
  939.               = cache->semifree_superstate->prev_recyclable;
  940.             superstate->next_recyclable->prev_recyclable
  941.               = superstate;
  942.             superstate->prev_recyclable->next_recyclable
  943.               = superstate;
  944.             cache->semifree_superstate = superstate;
  945.           }
  946.         ++cache->semifree_superstates;
  947.           }
  948.         }
  949.       set->superstate = 0;
  950.       goto handle_cache_miss;
  951.     }
  952.       ++cache->hits;
  953.       superstate = set->superstate;
  954.  
  955.       rx_refresh_this_superstate (cache, superstate);
  956.       return superstate;
  957.     }
  958.  
  959.  handle_cache_miss:
  960.  
  961.   /* This point reached only for cache misses. */
  962.   ++cache->misses;
  963. #if RX_DEBUG
  964.   if (rx_debug_trace > 1)
  965.     {
  966.       struct rx_superset * setp = set;
  967.       fprintf (stderr, "Building a superstet %d(%d): ", rx->rx_id, set);
  968.       while (setp)
  969.     {
  970.       fprintf (stderr, "%d ", setp->id);
  971.       setp = setp->cdr;
  972.     }
  973.       fprintf (stderr, "(%d)\n", set);
  974.     }
  975. #endif
  976.   superstate = (struct rx_superstate *)rx_cache_get_superstate (cache);
  977.   if (!superstate)
  978.     return 0;
  979.  
  980.   if (!cache->lru_superstate)
  981.     (cache->lru_superstate
  982.      = superstate->next_recyclable
  983.      = superstate->prev_recyclable
  984.      = superstate);
  985.   else
  986.     {
  987.       superstate->next_recyclable = cache->lru_superstate;
  988.       superstate->prev_recyclable = cache->lru_superstate->prev_recyclable;
  989.       (  superstate->prev_recyclable->next_recyclable
  990.        = superstate->next_recyclable->prev_recyclable
  991.        = superstate);
  992.     }
  993.   superstate->rx_id = rx->rx_id;
  994.   superstate->transition_refs = 0;
  995.   superstate->locks = 0;
  996.   superstate->is_semifree = 0;
  997.   set->superstate = superstate;
  998.   superstate->contents = set;
  999.   rx_protect_superset (rx, set);
  1000.   superstate->edges = 0;
  1001.   {
  1002.     int x;
  1003.     /* None of the transitions from this superstate are known yet. */
  1004.     for (x = 0; x < rx->local_cset_size; ++x) /* &&&&& 3.8 % */
  1005.       {
  1006.     struct rx_inx * ifr = &superstate->transitions[x];
  1007.     ifr->inx = rx->instruction_table [rx_cache_miss];
  1008.     ifr->data = ifr->data_2 = 0;
  1009.       }
  1010.   }
  1011.   return superstate;
  1012. }
  1013.  
  1014.  
  1015. /* This computes the destination set of one edge of the superstate NFA.
  1016.  * Note that a RX_DISTINCT_FUTURE is a superstate edge.
  1017.  * Returns 0 on an allocation failure.
  1018.  */
  1019.  
  1020. #ifdef __STDC__
  1021. static int 
  1022. solve_destination (struct rx *rx, struct rx_distinct_future *df)
  1023. #else
  1024. static int 
  1025. solve_destination (rx, df)
  1026.      struct rx *rx;
  1027.      struct rx_distinct_future *df;
  1028. #endif
  1029. {
  1030.   struct rx_super_edge *tc = df->edge;
  1031.   struct rx_superset *nfa_state;
  1032.   struct rx_superset *nil_set = rx_superset_cons (rx, 0, 0);
  1033.   struct rx_superset *solution = nil_set;
  1034.   struct rx_superstate *dest;
  1035.  
  1036.   /* Iterate over all NFA states in the state set of this superstate. */
  1037.   for (nfa_state = df->present->contents;
  1038.        nfa_state->car;
  1039.        nfa_state = nfa_state->cdr)
  1040.     {
  1041.       struct rx_nfa_edge *e;
  1042.       /* Iterate over all edges of each NFA state. */
  1043.       for (e = nfa_state->car->edges; e; e = e->next)
  1044.         /* If we find an edge that is labeled with 
  1045.      * the characters we are solving for.....
  1046.      */
  1047.     if (rx_bitset_is_subset (rx->local_cset_size,
  1048.                  tc->cset, e->params.cset))
  1049.       {
  1050.         struct rx_nfa_state *n = e->dest;
  1051.         struct rx_possible_future *pf;
  1052.         /* ....search the partial epsilon closures of the destination
  1053.          * of that edge for a path that involves the same set of
  1054.          * side effects we are solving for.
  1055.          * If we find such a RX_POSSIBLE_FUTURE, we add members to the
  1056.          * stateset we are computing.
  1057.          */
  1058.         for (pf = n->futures; pf; pf = pf->next)
  1059.           if (pf->effects == df->effects)
  1060.         {
  1061.           struct rx_superset * old_sol = solution;
  1062.           rx_protect_superset (rx, solution);
  1063.           solution =
  1064.             rx_superstate_eclosure_union (rx, solution, pf->destset);
  1065.           if (!solution)
  1066.             return 0;
  1067.           rx_release_superset (rx, old_sol);
  1068.         }
  1069.       }
  1070.     }
  1071.   /* It is possible that the RX_DISTINCT_FUTURE we are working on has 
  1072.    * the empty set of NFA states as its definition.  In that case, this
  1073.    * is a failure point.
  1074.    */
  1075.   if (solution == nil_set)
  1076.     {
  1077.       df->future_frame.inx = (void *) rx_backtrack;
  1078.       df->future_frame.data = 0;
  1079.       df->future_frame.data_2 = 0;
  1080.       return 1;
  1081.     }
  1082.   rx_protect_superset (rx, solution);
  1083.   dest = rx_superstate (rx, solution);
  1084.   rx_release_superset (rx, solution);
  1085.   if (!dest)
  1086.     return 0;
  1087.  
  1088.   {
  1089.     struct rx_distinct_future *dft;
  1090.     dft = df;
  1091.     df->prev_same_dest->next_same_dest = 0;
  1092.     while (dft)
  1093.       {
  1094.     dft->future = dest;
  1095.     dft->future_frame.inx = rx->instruction_table[rx_next_char];
  1096.     dft->future_frame.data = (void *) dest->transitions;
  1097.     dft = dft->next_same_dest;
  1098.       }
  1099.     df->prev_same_dest->next_same_dest = df;
  1100.   }
  1101.   if (!dest->transition_refs)
  1102.     dest->transition_refs = df;
  1103.   else
  1104.     {
  1105.       struct rx_distinct_future *dft = dest->transition_refs->next_same_dest;
  1106.       dest->transition_refs->next_same_dest = df->next_same_dest;
  1107.       df->next_same_dest->prev_same_dest = dest->transition_refs;
  1108.       df->next_same_dest = dft;
  1109.       dft->prev_same_dest = df;
  1110.     }
  1111.   return 1;
  1112. }
  1113.  
  1114.  
  1115. /* This takes a superstate and a character, and computes some edges
  1116.  * from the superstate NFA.  In particular, this computes all edges
  1117.  * that lead from SUPERSTATE given CHR.   This function also 
  1118.  * computes the set of characters that share this edge set.
  1119.  * This returns 0 on allocation error.
  1120.  * The character set and list of edges are returned through 
  1121.  * the paramters CSETOUT and DFOUT.
  1122. } */
  1123.  
  1124. #ifdef __STDC__
  1125. static int 
  1126. compute_super_edge (struct rx *rx, struct rx_distinct_future **dfout,
  1127.               rx_Bitset csetout, struct rx_superstate *superstate,
  1128.               unsigned char chr)  
  1129. #else
  1130. static int 
  1131. compute_super_edge (rx, dfout, csetout, superstate, chr)
  1132.      struct rx *rx;
  1133.      struct rx_distinct_future **dfout;
  1134.      rx_Bitset csetout;
  1135.      struct rx_superstate *superstate;
  1136.      unsigned char chr;
  1137. #endif
  1138. {
  1139.   struct rx_superset *stateset = superstate->contents;
  1140.  
  1141.   /* To compute the set of characters that share edges with CHR, 
  1142.    * we start with the full character set, and subtract.
  1143.    */
  1144.   rx_bitset_universe (rx->local_cset_size, csetout);
  1145.   *dfout = 0;
  1146.  
  1147.   /* Iterate over the NFA states in the superstate state-set. */
  1148.   while (stateset->car)
  1149.     {
  1150.       struct rx_nfa_edge *e;
  1151.       for (e = stateset->car->edges; e; e = e->next)
  1152.     if (RX_bitset_member (e->params.cset, chr))
  1153.       {
  1154.        102, 103, 104, 105, 106, 107, 108, 109,
  1155.  110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
  1156.  120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
  1157.  130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
  1158.  140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
  1159.  150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  1160.  160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
  1161.  170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
  1162.  180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
  1163.  190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
  1164.  
  1165.  200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
  1166.  210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
  1167.  220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
  1168.  230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  1169.  240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
  1170.  250, 251, 252, 253, 254, 255
  1171. };
  1172.  
  1173. /* The compiler keeps an inverted translation table.
  1174.  * This looks up/inititalize elements.
  1175.  * VALID is an array of booleans that validate CACHE.
  1176.  */
  1177.  
  1178. #ifdef __STDC__
  1179. static rx_Bitset
  1180. inverse_translation (struct re_pattern_buffer * rxb,
  1181.              char * valid, rx_Bitset cache,
  1182.              char * translate, int c)
  1183. #else
  1184. static rx_Bitset
  1185. inverse_translation (rxb, valid, cache, translate, c)
  1186.      struct re_pattern_buffer * rxb;
  1187.      char * valid;
  1188.      rx_Bitset cache;
  1189.      char * translate;
  1190.      int c;
  1191. #endif
  1192. {
  1193.   rx_Bitset cs
  1194.     = cache + c * rx_bitset_numb_subsets (rxb->rx.local_cset_size); 
  1195.  
  1196.   if (!valid[c])
  1197.     {
  1198.       int x;
  1199.       int c_tr = TRANSLATE(c);
  1200.       rx_bitset_null (rxb->rx.local_cset_size, cs);
  1201.       for (x = 0; x < 256; ++x)    /* &&&& 13.37 */
  1202.     if (TRANSLATE(x) == c_tr)
  1203.       RX_bitset_enjoin (cs, x);
  1204.       valid[c] = 1;
  1205.     }
  1206.   return cs;
  1207. }
  1208.  
  1209.  
  1210.  
  1211.  
  1212. /* More subroutine declarations and macros for regex_compile.  */
  1213.  
  1214. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and 
  1215.    false if it's not.  */
  1216.  
  1217. #ifdef __STDC__
  1218. static boolean
  1219. group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
  1220. #else
  1221. static boolean
  1222. group_in_compile_stack (compile_stack, regnum)
  1223.     compile_stack_type compile_stack;
  1224.     regnum_t regnum;
  1225. #endif
  1226. {
  1227.   int this_element;
  1228.  
  1229.   for (this_element = compile_stack.avail - 1;  
  1230.        this_element >= 0; 
  1231.        this_element--)
  1232.     if (compile_stack.stack[this_element].regnum == regnum)
  1233.       return true;
  1234.  
  1235.   return false;
  1236. }
  1237.  
  1238.  
  1239. /*
  1240.  * Read the ending character of a range (in a bracket expression) from the
  1241.  * uncompiled pattern *P_PTR (which ends at PEND).  We assume the
  1242.  * starting character is in `P[-2]'.  (`P[-1]' is the character `-'.)
  1243.  * Then we set the translation of all bits between the starting and
  1244.  * ending characters (inclusive) in the compiled pattern B.
  1245.  * 
  1246.  * Return an error code.
  1247.  * 
  1248.  * We use these short variable names so we can use the same macros as
  1249.  * `regex_compile' itself.  
  1250.  */
  1251.  
  1252. #ifdef __STDC__
  1253. static reg_errcode_t
  1254. compile_range (struct re_pattern_buffer * rxb, rx_Bitset cs,
  1255.            const char ** p_ptr, const char * pend,
  1256.            char * translate, reg_syntax_t syntax,
  1257.            rx_Bitset inv_tr,  char * valid_inv_tr)
  1258. #else
  1259. static reg_errcode_t
  1260. compile_range (rxb, cs, p_ptr, pend, translate, syntax, inv_tr, valid_inv_tr)
  1261.      struct re_pattern_buffer * rxb;
  1262.      rx_Bitset cs;
  1263.      const char ** p_ptr;
  1264.      const char * pend;
  1265.      char * translate;
  1266.      reg_syntax_t syntax;
  1267.      rx_Bitset inv_tr;
  1268.      char * valid_inv_tr;
  1269. #endif
  1270. {
  1271.   unsigned this_char;
  1272.  
  1273.   const char *p = *p_ptr;
  1274.  
  1275.   unsigned char range_end;
  1276.   unsigned char range_start = TRANSLATE(p[-2]);
  1277.  
  1278.   if (p == pend)
  1279.     return REG_ERANGE;
  1280.  
  1281.   PATFETCH (range_end);
  1282.  
  1283.   (*p_ptr)++;
  1284.  
  1285.   if (range_start > range_end)
  1286.     return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  1287.  
  1288.   for (this_char = range_start; this_char <= range_end; this_char++)
  1289.     {
  1290.       rx_Bitset it =
  1291.     inverse_translation (rxb, valid_inv_tr, inv_tr, translate, this_char);
  1292.       rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  1293.     }
  1294.   
  1295.   return REG_NOERROR;
  1296. }
  1297.  
  1298.  
  1299. /* This searches a regexp for backreference side effects.
  1300.  * It fills in the array OUT with 1 at the index of every register pair
  1301.  * referenced by a backreference.
  1302.  *
  1303.  * This is used to help optimize patterns for searching.  The information is
  1304.  * useful because, if the caller doesn't want register values, backreferenced
  1305.  * registers are the only registers for which we need rx_backtrack.
  1306.  */
  1307.  
  1308. #ifdef __STDC__
  1309. static void
  1310. find_backrefs (char * out, struct rexp_node * rexp,
  1311.            struct re_se_params * params)
  1312. #else
  1313. static void
  1314. find_backrefs (out, rexp, params)
  1315.      char * out;
  1316.      struct rexp_node * rexp;
  1317.      struct re_se_params * params;
  1318. #endif
  1319. {
  1320.   if (rexp)
  1321.     switch (rexp->type)
  1322.       {
  1323.       case r_cset:
  1324.       case r_data:
  1325.     return;
  1326.       case r_alternate:
  1327.       case r_concat:
  1328.       case r_opt:
  1329.       case r_star:
  1330.       case r_2phase_star:
  1331.     find_backrefs (out, rexp->params.pair.left, params);
  1332.     find_backrefs (out, rexp->params.pair.right, params);
  1333.     return;
  1334.       case r_side_effect:
  1335.     if (   ((int)rexp->params.side_effect >= 0)
  1336.         && (params [(int)rexp->params.side_effect].se == re_se_backref))
  1337.       out[ params [(int)rexp->params.side_effect].op1] = 1;
  1338.     return;
  1339.       }
  1340. }
  1341.  
  1342.  
  1343.  
  1344. /* Returns 0 unless the pattern can match the empty string. */
  1345.  
  1346. #ifdef __STDC__
  1347. static int
  1348. compute_fastset (struct re_pattern_buffer * rxb, struct rexp_node * rexp)
  1349. #else
  1350. static int
  1351. compute_fastset (rxb, rexp)
  1352.      struct re_pattern_buffer * rxb;
  1353.      struct rexp_node * rexp;
  1354. #endif
  1355. {
  1356.   if (!rexp)
  1357.     return 1;
  1358.   switch (rexp->type)
  1359.     {
  1360.     case r_data:
  1361.       return 1;
  1362.     case r_cset:
  1363.       {
  1364.     rx_bitset_union (rxb->rx.local_cset_size,
  1365.              rxb->fastset, rexp->params.cset);
  1366.       }
  1367.       return 0;
  1368.     case r_concat:
  1369.       return (compute_fastset (rxb, rexp->params.pair.left)
  1370.           && compute_fastset (rxb, rexp->params.pair.right));
  1371.     case r_2phase_star:
  1372.       compute_fastset (rxb, rexp->params.pair.left);
  1373.       /* compute_fastset (rxb, rexp->params.pair.right);  nope... */
  1374.       return 1;
  1375.     case r_alternate:
  1376.       return !!(compute_fastset (rxb, rexp->params.pair.left)
  1377.         + compute_fastset (rxb, rexp->params.pair.right));
  1378.     case r_opt:
  1379.     case r_star:
  1380.       compute_fastset (rxb, rexp->params.pair.left);
  1381.       return 1;
  1382.     case r_side_effect:
  1383.       return 1;
  1384.     }
  1385. }
  1386.  
  1387.  
  1388. /* returns
  1389.  *  1 -- yes, definately anchored by the given side effect.
  1390.  *  2 -- maybe anchored, maybe the empty string.
  1391.  *  0 -- definately not anchored
  1392.  *  There is simply no other possibility.
  1393.  */
  1394.  
  1395. #ifdef __STDC__
  1396. static int
  1397. is_anchored (struct rexp_node * rexp, rx_side_effect se)
  1398. #else
  1399. static int
  1400. is_anchored (rexp, se)
  1401.      struct rexp_node * rexp;
  1402.      rx_side_effect se;
  1403. #endif
  1404. {
  1405.   if (!rexp)
  1406.     return 2;
  1407.   switch (rexp->type)
  1408.     {
  1409.     case r_cset:
  1410.     case r_data:
  1411.       return 0;
  1412.     case r_concat:
  1413.     case r_2phase_star:
  1414.       {
  1415.     int l = is_anchored (rexp->params.pair.left, se);
  1416.     return (l == 2 ? is_anchored (rexp->params.pair.right, se) : l);
  1417.       }
  1418.     case r_alternate:
  1419.       {
  1420.     int l = is_anchored (rexp->params.pair.left, se);
  1421.     int r = l ? is_anchored (rexp->params.pair.right, se) : 0;
  1422.     return MAX (l, r);
  1423.       }
  1424.     case r_opt:
  1425.     case r_star:
  1426.       return is_anchored (rexp->params.pair.left, se) ? 2 : 0;
  1427.       
  1428.     case r_side_effect:
  1429.       return ((rexp->params.side_effect == se)
  1430.           ? 1 : 2);
  1431.     }
  1432. }
  1433.  
  1434.  
  1435. /* This removes register assignments that aren't required by backreferencing.
  1436.  * This can speed up explore_future, especially if it eliminates
  1437.  * non-determinism in the superstate NFA.
  1438.  * 
  1439.  * NEEDED is an array of characters, presumably filled in by FIND_BACKREFS.
  1440.  * The non-zero elements of the array indicate which register assignments
  1441.  * can NOT be removed from the expression.
  1442.  */
  1443.  
  1444. #ifdef __STDC__
  1445. static struct rexp_node *
  1446. remove_unecessary_side_effects (struct rx * rx, char * needed,
  1447.                 struct rexp_node * rexp,
  1448.                 struct re_se_params * params)
  1449. #else
  1450. static struct rexp_node *
  1451. remove_unecessary_side_effects (rx, needed, rexp, params)
  1452.      struct rx * rx;
  1453.      char * needed;
  1454.      struct rexp_node * rexp;
  1455.      struct re_se_params * params;
  1456. #endif
  1457. {
  1458.   struct rexp_node * l;
  1459.   struct rexp_node * r;
  1460.   if (!rexp)
  1461.     return 0;
  1462.   else
  1463.     switch (rexp->type)
  1464.       {
  1465.       case r_cset:
  1466.       case r_data:
  1467.     return rexp;
  1468.       case r_alternate:
  1469.       case r_concat:
  1470.       case r_X_WANT_SE_DEFS
  1471.   23
  1472. };
  1473.  
  1474. static char idempotent_se[] =
  1475. {
  1476.   13,
  1477. #define RX_WANT_SE_DEFS 1
  1478. #undef RX_DEF_SE
  1479. #undef RX_DEF_CPLX_SE
  1480. #define RX_DEF_SE(IDEM, NAME, VALUE)          IDEM,
  1481. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     
  1482. #include "rx.h"
  1483. #undef RX_DEF_SE
  1484. #undef RX_DEF_CPLX_SE
  1485. #undef RX_WANT_SE_DEFS
  1486.   42
  1487. };
  1488.  
  1489.  
  1490.  
  1491.  
  1492. #ifdef __STDC__
  1493. static int
  1494. has_any_se (struct rx * rx,
  1495.         struct rexp_node * rexp)
  1496. #else
  1497. static int
  1498. has_any_se (rx, rexp)
  1499.      struct rx * rx;
  1500.      struct rexp_node * rexp;
  1501. #endif
  1502. {
  1503.   if (!rexp)
  1504.     return 0;
  1505.  
  1506.   switch (rexp->type)
  1507.     {
  1508.     case r_cset:
  1509.     case r_data:
  1510.       return 0;
  1511.  
  1512.     case r_side_effect:
  1513.       return 1;
  1514.       
  1515.     case r_2phase_star:
  1516.     case r_concat:
  1517.     case r_alternate:
  1518.       return
  1519.     (   has_any_se (rx, rexp->params.pair.left)
  1520.      || has_any_se (rx, rexp->params.pair.right));
  1521.  
  1522.     case r_opt:
  1523.     case r_star:
  1524.       return has_any_se (rx, rexp->params.pair.left);
  1525.     }
  1526. }
  1527.  
  1528.  
  1529.  
  1530. /* This must be called AFTER `convert_hard_loops' for a given REXP. */
  1531. #ifdef __STDC__
  1532. static int
  1533. has_non_idempotent_epsilon_path (struct rx * rx,
  1534.                  struct rexp_node * rexp,
  1535.                  struct re_se_params * params)
  1536. #else
  1537. static int
  1538. has_non_idempotent_epsilon_path (rx, rexp, params)
  1539.      struct rx * rx;
  1540.      struct rexp_node * rexp;
  1541.      struct re_se_params * params;
  1542. #endif
  1543. {
  1544.   if (!rexp)
  1545.     return 0;
  1546.  
  1547.   switch (rexp->type)
  1548.     {
  1549.     case r_cset:
  1550.     case r_data:
  1551.     case r_star:
  1552.       return 0;
  1553.  
  1554.     case r_side_effect:
  1555.       return
  1556.     !((int)rexp->params.side_effect > 0
  1557.       ? idempotent_complex_se [ params [(int)rexp->params.side_effect].se ]
  1558.       : idempotent_se [-(int)rexp->params.side_effect]);
  1559.       
  1560.     case r_alternate:
  1561.       return
  1562.     (   has_non_idempotent_epsilon_path (rx,
  1563.                          rexp->params.pair.left, params)
  1564.      || has_non_idempotent_epsilon_path (rx,
  1565.                          rexp->params.pair.right, params));
  1566.  
  1567.     case r_2phase_star:
  1568.     case r_concat:
  1569.       return
  1570.     (   has_non_idempotent_epsilon_path (rx,
  1571.                          rexp->params.pair.left, params)
  1572.      && has_non_idempotent_epsilon_path (rx,
  1573.                          rexp->params.pair.right, params));
  1574.  
  1575.     case r_opt:
  1576.       return has_non_idempotent_epsilon_path (rx,
  1577.                           rexp->params.pair.left, params);
  1578.     }
  1579.  
  1580. }
  1581.  
  1582.  
  1583.  
  1584. /* This computes rougly what it's name suggests.   It can (and does) go wrong 
  1585.  * in the direction of returning spurious 0 without causing disasters.
  1586.  */
  1587. #ifdef __STDC__
  1588. static int
  1589. begins_with_complex_se (struct rx * rx, struct rexp_node * rexp)
  1590. #else
  1591. static int
  1592. begins_with_complex_se (rx, rexp)
  1593.      struct rx * rx;
  1594.      struct rexp_node * rexp;
  1595. #endif
  1596. {
  1597.   if (!rexp)
  1598.     return 0;
  1599.  
  1600.   switch (rexp->type)
  1601.     {
  1602.     case r_cset:
  1603.     case r_data:
  1604.       return 0;
  1605.  
  1606.     case r_side_effect:
  1607.       return ((int)rexp->params.side_effect >= 0);
  1608.       
  1609.     case r_alternate:
  1610.       return
  1611.     (   begins_with_complex_se (rx, rexp->params.pair.left)
  1612.      && begins_with_complex_se (rx, rexp->params.pair.right));
  1613.  
  1614.  
  1615.     case r_concat:
  1616.       return has_any_se (rx, rexp->params.pair.left);
  1617.     case r_opt:
  1618.     case r_star:
  1619.     case r_2phase_star:
  1620.       return 0;
  1621.     }
  1622. }
  1623.  
  1624.  
  1625. /* This destructively removes some of the re_se_tv side effects from 
  1626.  * a rexp tree.  In particular, during parsing re_se_tv was inserted on the
  1627.  * right half of every | to guarantee that posix path preference could be 
  1628.  * honored.  This function removes some which it can be determined aren't 
  1629.  * needed.  
  1630.  */
  1631.  
  1632. #ifdef __STDC__
  1633. static void
  1634. speed_up_alt (struct rx * rx,
  1635.           struct rexp_node * rexp,
  1636.           int unposix)
  1637. #else
  1638. static void
  1639. speed_up_alt (rx, rexp, unposix)
  1640.      struct rx * rx;
  1641.      struct rexp_node * rexp;
  1642.      int unposix;
  1643. #endif
  1644. {
  1645.   if (!rexp)
  1646.     return;
  1647.  
  1648.   switch (rexp->type)
  1649.     {
  1650.     case r_cset:
  1651.     case r_data:
  1652.     case r_side_effect:
  1653.       return;
  1654.  
  1655.     case r_opt:
  1656.     case r_star:
  1657.       speed_up_alt (rx, rexp->params.pair.left, unposix);
  1658.       return;
  1659.  
  1660.     case r_2phase_star:
  1661.     case r_concat:
  1662.       speed_up_alt (rx, rexp->params.pair.left, unposix);
  1663.       speed_up_alt (rx, rexp->params.pair.right, unposix);
  1664.       return;
  1665.  
  1666.     case r_alternate:
  1667.       /* the right child is guaranteed to be (concat re_se_tv <subexp>) */
  1668.  
  1669.       speed_up_alt (rx, rexp->params.pair.left, unposix);
  1670.       speed_up_alt (rx, rexp->params.pair.right->params.pair.right, unposix);
  1671.       
  1672.       if (   unposix
  1673.       || (begins_with_complex_se
  1674.           (rx, rexp->params.pair.right->params.pair.right))
  1675.       || !(   has_any_se (rx, rexp->params.pair.right->params.pair.right)
  1676.            || has_any_se (rx, rexp->params.pair.left)))
  1677.     {
  1678.       struct rexp_node * conc = rexp->params.pair.right;
  1679.       rexp->params.pair.right = conc->params.pair.right;
  1680.       conc->params.pair.right = 0;
  1681.       rx_free_rexp (rx, conc);
  1682.     }
  1683.     }
  1684. }
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1691.    Returns one of error codes defined in `regex.h', or zero for success.
  1692.  
  1693.    Assumes the `allocated' (and perhaps `buffer') and `translate'
  1694.    fields are set in BUFP on entry.
  1695.  
  1696.    If it succeeds, results are put in BUFP (if it returns an error, the
  1697.    contents of BUFP are undefined):
  1698.      `buffer' is the compiled pattern;
  1699.      `syntax' is set to SYNTAX;
  1700.      `used' is set to the length of the compiled pattern;
  1701.      `fastmap_accurate' is set to zero;
  1702.      `re_nsub' is set to the number of groups in PATTERN;
  1703.      `not_bol' and `not_eol' are set to zero.
  1704.    
  1705.    The `fastmap' and `newline_anchor' fields are neither
  1706.    examined nor set.  */
  1707.  
  1708.  
  1709.  
  1710. #ifdef __STDC__
  1711. reg_errcode_t
  1712. rx_compile (const char *pattern, int size,
  1713.         reg_syntax_t syntax,
  1714.         struct re_pattern_buffer * rxb) 
  1715. #else
  1716. reg_errcode_t
  1717. rx_compile (pattern, size, syntax, rxb)
  1718.      const char *pattern;
  1719.      int size;
  1720.      reg_syntax_t syntax;
  1721.      struct re_pattern_buffer * rxb;
  1722. #endif
  1723. {
  1724.   RX_subset
  1725.     inverse_translate [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  1726.   char
  1727.     validate_inv_tr [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  1728.  
  1729.   /* We fetch characters from PATTERN here.  Even though PATTERN is
  1730.      `char *' (i.e., signed), we declare these variables as unsigned, so
  1731.      they can be reliably used as array indices.  */
  1732.   register unsigned char c, c1;
  1733.   
  1734.   /* A random tempory spot in PATTERN.  */
  1735.   const char *p1;
  1736.   
  1737.   /* Keeps track of unclosed groups.  */
  1738.   compile_stack_type compile_stack;
  1739.  
  1740.   /* Points to the current (ending) position in the pattern.  */
  1741.   const char *p = pattern;
  1742.   const char *pend = pattern + size;
  1743.   
  1744.   /* How to translate the characters in the pattern.  */
  1745.   char *translate = rxb->translate ? rxb->translate : id_translation;
  1746.  
  1747.   /* When parsing is done, this will hold the expression tree. */
  1748.   struct rexp_node * rexp = 0;
  1749.  
  1750.   /* In the midst of compilation, this holds onto the regexp 
  1751.    * first parst while rexp goes on to aquire additional constructs.
  1752.    */
  1753.   struct rexp_node * orig_rexp = 0;
  1754.   struct rexp_node * fewer_side_effects = 0;
  1755.  
  1756.   /* This and top_expression are saved on the compile stack. */
  1757.   struct rexp_node ** top_expression = &rexp;
  1758.   struct rexp_node ** last_expression = top_expression;
  1759.   
  1760.   /* Parameter to `goto append_node' */
  1761.   struct rexp_node * append;
  1762.  
  1763.   /* Counts open-groups as they are encountered.  This is the index of the
  1764.    * innermost group being compiled.
  1765.    */
  1766.   regnum_t regnum = 0;
  1767.  
  1768.   /* Place in the uncompiled pattern (i.e., the {) to
  1769.    * which to go back if the interval is invalid.  
  1770.    */
  1771.   const char *beg_interval;
  1772.  
  1773.   struct re_se_params * params = 0;
  1774.   int paramc = 0;        /* How many complex side effects so far? */
  1775.  
  1776.   rx_side_effect side;        /* param to `goto add_side_effect' */
  1777.  
  1778.   bzero (validate_inv_tr, sizeof (validate_inv_tr));
  1779.  
  1780.   rxb->rx.instruction_table = rx_id_instruction_table;
  1781.  
  1782.  
  1783.   /* Initialize the compile stack.  */
  1784.   compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1785.   if (compile_stack.stack == 0)
  1786.     return REG_ESPACE;
  1787.  
  1788.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1789.   compile_stack.avail = 0;
  1790.  
  1791.   /* Initialize the pattern buffer.  */
  1792.   rxb->rx.cache = &default_cache;
  1793.   rxb->syntax = syntax;
  1794.   rxb->fastmap_accurate = 0;
  1795.   rxb->not_bol = rxb->not_eol = 0;
  1796.   rxb->least_subs = 0;
  1797.   
  1798.   /* Always count groups, whether or not rxb->no_sub is set.  
  1799.    * The whole pattern is implicitly group 0, so counting begins
  1800.    * with 1.
  1801.    */
  1802.   rxb->re_nsub = 0;
  1803.  
  1804. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  1805.   /* Initialize the syntax table.  */
  1806.    init_syntax_once ();
  1807. #endif
  1808.  
  1809.   /* Loop through the uncompiled pattern until we're at the end.  */
  1810.   while (p != pend)
  1811.     {
  1812.       PATFETCH (c);
  1813.  
  1814.       switch (c)
  1815.         {
  1816.         case '^':
  1817.           {
  1818.             if (   /* If at start of pattern, it's an operator.  */
  1819.                    p == pattern + 1
  1820.                    /* If context independent, it's an operator.  */
  1821.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1822.                    /* Otherwise, depends on what's come before.  */
  1823.                 || at_begline_loc_p (pattern, p, syntax))
  1824.           {
  1825.         struct rexp_node * n
  1826.           = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_hat);
  1827.         if (!n)
  1828.           return REG_ESPACE;
  1829.         append = n;
  1830.         goto append_node;
  1831.           }
  1832.             else
  1833.               goto normal_char;
  1834.           }
  1835.           break;
  1836.  
  1837.  
  1838.         case '$':
  1839.           {
  1840.             if (   /* If at end of pattern, it's an operator.  */
  1841.                    p == pend 
  1842.                    /* If context independent, it's an operator.  */
  1843.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1844.                    /* Otherwise, depends on what's next.  */
  1845.                 || at_endline_loc_p (p, pend, syntax))
  1846.           {
  1847.         struct rexp_node * n
  1848.           = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_dollar);
  1849.         if (!n)
  1850.           return REG_ESPACE;
  1851.         append = n;
  1852.         goto append_node;
  1853.           }
  1854.              else
  1855.                goto normal_char;
  1856.            }
  1857.            break;
  1858.  
  1859.  
  1860.     case '+':
  1861.         case '?':
  1862.           if ((syntax & RE_BK_PLUS_QM)
  1863.               || (syntax & RE_LIMITED_OPS))
  1864.             goto normal_char;
  1865.  
  1866.         handle_plus:
  1867.         case '*':
  1868.           /* If there is no previous pattern... */
  1869.           if (pointless_if_repeated (*last_expression, params))
  1870.             {
  1871.               if (syntax & RE_CONTEXT_INVALID_OPS)
  1872.                 return REG_BADRPT;
  1873.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1874.                 goto normal_char;
  1875.             }
  1876.  
  1877.           {
  1878.             /* 1 means zero (many) matches is allowed.  */
  1879.             char zero_times_ok = 0, many_times_ok = 0;
  1880.  
  1881.             /* If there is a sequence of repetition chars, collapse it
  1882.                down to just one (the right one).  We can't combine
  1883.                interval operators with these because of, e.g., `a{2}*',
  1884.                which should only match an even number of `a's.  */
  1885.  
  1886.             for (;;)
  1887.               {
  1888.                 zero_times_ok |= c != '+';
  1889.                 many_times_ok |= c != '?';
  1890.  
  1891.                 if (p == pend)
  1892.                   break;
  1893.  
  1894.                 PATFETCH (c);
  1895.  
  1896.                 if (c == '*'
  1897.                     || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1898.                   ;
  1899.  
  1900.                 else if (syntax & RE_BK_PLUS_QM  &&  c == '\\')
  1901.                   {
  1902.                     if (p == pend) return REG_EESCAPE;
  1903.  
  1904.                     PATFETCH (c1);
  1905.                     if (!(c1 == '+' || c1 == '?'))
  1906.                       {
  1907.                         PATUNFETCH;
  1908.                         PATUNFETCH;
  1909.                         break;
  1910.                       }
  1911.  
  1912.                     c = c1;
  1913.                   }
  1914.                 else
  1915.                   {
  1916.                     PATUNFETCH;
  1917.                     break;
  1918.                   }
  1919.  
  1920.                 /* If we get here, we found another repeat character.  */
  1921.                }
  1922.  
  1923.             /* Star, etc. applied to an empty pattern is equivalent
  1924.                to an empty pattern.  */
  1925.             if (!last_expression)
  1926.               break;
  1927.  
  1928.         /* Now we know whether or not zero matches is allowed
  1929.          * and also whether or not two or more matches is allowed.
  1930.          */
  1931.  
  1932.         {
  1933.           struct rexp_node * inner_exp = *last_expression;
  1934.           int need_sync = 0;
  1935.  
  1936.           if (many_times_ok
  1937.           && has_non_idempotent_epsilon_path (&rxb->rx,
  1938.                               inner_exp, params))
  1939.         {
  1940.           struct rexp_node * pusher
  1941.             = rx_mk_r_side_effect (&rxb->rx,
  1942.                        (rx_side_effect)re_se_pushpos);
  1943.           struct rexp_node * checker
  1944.             = rx_mk_r_side_effect (&rxb->rx,
  1945.                        (rx_side_effect)re_se_chkpos);
  1946.           struct rexp_node * pushback
  1947.             = rx_mk_r_side_effect (&rxb->rx,
  1948.                        (rx_side_effect)re_se_pushback);
  1949.           rx_Bitset cs = rx_cset (&rxb->rx);
  1950.           struct rexp_node * lit_t = rx_mk_r_cset (&rxb->rx, cs);
  1951.           struct rexp_node * fake_state
  1952.             = rx_mk_r_concat (&rxb->rx, pushback, lit_t);
  1953.           struct rexp_node * phase2
  1954.             = rx_mk_r_concat (&rxb->rx, checker, fake_state);
  1955.           struct rexp_node * popper
  1956.             = rx_mk_r_side_effect (&rxb->rx,
  1957.                        (rx_side_effect)re_se_poppos);
  1958.           struct rexp_node * star
  1959.             = rx_mk_r_2phase_star (&rxb->rx, inner_exp, phase2);
  1960.           struct rexp_node * a
  1961.             = rx_mk_r_concat (&rxb->rx, pusher, star);
  1962.           struct rexp_node * whole_thing
  1963.             = rx_mk_r_concat (&rxb->rx, a, popper);
  1964.           if (!(pusher && star && pushback && lit_t && fake_state
  1965.             && lit_t && phase2 && checker && popper
  1966.             && a && whole_thing))
  1967.             return REG_ESPACE;
  1968.           RX_bitset_enjoin (cs, 't');
  1969.           *last_expression = whole_thing;
  1970.         }
  1971.           else
  1972.         {
  1973.           struct rexp_node * star =
  1974.             (many_times_ok ? rx_mk_r_star : rx_mk_r_opt)
  1975.               (&rxb->rx, *last_expression);
  1976.           if (!star)
  1977.             return REG_ESPACE;
  1978.           *last_expression = star;
  1979.           need_sync = has_any_se (&rxb->rx, *last_expression);
  1980.         }
  1981.           if (!zero_times_ok)
  1982.         {
  1983.           struct rexp_node * concat
  1984.             = rx_mk_r_concat (&rxb->rx, inner_exp,
  1985.                       rx_copy_rexp (&rxb->rx,
  1986.                             *last_expression));
  1987.           if (!concat)
  1988.             return REG_ESPACE;
  1989.           *last_expression = concat;
  1990.         }
  1991.           if (need_sync)
  1992.         {
  1993.           int sync_se = paramc;
  1994.           params = (params
  1995.                 ? ((struct re_se_params *)
  1996.                    realloc (params,
  1997.                     sizeof (*params) * (1 + paramc)))
  1998.                 : ((struct re_se_params *)
  1999.                    malloc (sizeof (*params))));
  2000.           if (!params)
  2001.             return REG_ESPACE;
  2002.           ++paramc;
  2003.           params [sync_se].se = re_se_tv;
  2004.           side = (rx_side_effect)sync_se;
  2005.           goto add_side_effect;
  2006.         }
  2007.         }
  2008.         /* The old regex.c used to optimize `.*\n'.  
  2009.          * Maybe rx should too?
  2010.          */
  2011.       }
  2012.       break;
  2013.  
  2014.  
  2015.     case '.':
  2016.       {
  2017.         rx_Bitset cs = rx_cset (&rxb->rx);
  2018.         struct rexp_node * n = rx_mk_r_cset (&rxb->rx, cs);
  2019.         if (!(cs && n))
  2020.           return REG_ESPACE;
  2021.  
  2022.         rx_bitset_universe (rxb->rx.local_cset_size, cs);
  2023.         if (!(rxb->syntax & RE_DOT_NEWLINE))
  2024.           RX_bitset_remove (cs, '\n');
  2025.         if (!(rxb->syntax & RE_DOT_NOT_NULL))
  2026.           RX_bitset_remove (cs, 0);
  2027.  
  2028.         append = n;
  2029.         goto append_node;
  2030.         break;
  2031.       }
  2032.  
  2033.  
  2034.         case '[':
  2035.       if (p == pend) return REG_EBRACK;
  2036.           {
  2037.             boolean had_char_class = false;
  2038.         rx_Bitset cs = rx_cset (&rxb->rx);
  2039.         struct rexp_node * node = rx_mk_r_cset (&rxb->rx, cs);
  2040.         int is_inverted = *p == '^';
  2041.         
  2042.         if (!(node && cs))
  2043.           return REG_ESPACE;
  2044.         
  2045.         /* This branch of the switch is normally exited with
  2046.          *`goto append_node'
  2047.          */
  2048.         append = node;
  2049.         
  2050.             if (is_inverted)
  2051.           p++;
  2052.         
  2053.             /* Remember the first position in the bracket expression.  */
  2054.             p1 = p;
  2055.         
  2056.             /* Read in characters and ranges, setting map bits.  */
  2057.             for (;;)
  2058.               {
  2059.                 if (p == pend) return REG_EBRACK;
  2060.         
  2061.                 PATFETCH (c);
  2062.         
  2063.                 /* \ might escape characters inside [...] and [^...].  */
  2064.                 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  2065.                   {
  2066.                     if (p == pend) return REG_EESCAPE;
  2067.             
  2068.                     PATFETCH (c1);
  2069.             {
  2070.               rx_Bitset it = inverse_translation (rxb, 
  2071.                               validate_inv_tr,
  2072.                               inverse_translate,
  2073.                               translate,
  2074.                               c1);
  2075.               rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  2076.             }
  2077.                     continue;
  2078.                   }
  2079.         
  2080.                 /* Could be the end of the bracket expression.  If it's
  2081.                    not (i.e., when the bracket expression is `[]' so
  2082.                    far), the ']' character bit gets set way below.  */
  2083.                 if (c == ']' && p != p1 + 1)
  2084.                   goto finalize_class_and_append;
  2085.         
  2086.                 /* Look ahead to see if it's a range when the last thing
  2087.                    was a character class.  */
  2088.                 if (had_char_class && c == '-' && *p != ']')
  2089.                   return REG_ERANGE;
  2090.         
  2091.                 /* Look ahead to see if it's a range when the last thing
  2092.                    was a character: if this is a hyphen not at the
  2093.                    beginning or the end of a list, then it's the range
  2094.                    operator.  */
  2095.                 if (c == '-' 
  2096.                     && !(p - 2 >= pattern && p[-2] == '[') 
  2097.                     && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  2098.                     && *p != ']')
  2099.                   {
  2100.                     reg_errcode_t ret
  2101.                       = compile_range (rxb, cs, &p, pend, translate, syntax,
  2102.                        inverse_translate, validate_inv_tr);
  2103.                     if (ret != REG_NOERROR) return ret;
  2104.                   }
  2105.         
  2106.                 else if (p[0] == '-' && p[1] != ']')
  2107.                   { /* This handles ranges made up of characters only.  */
  2108.                     reg_errcode_t ret;
  2109.             
  2110.             /* Move past the `-'.  */
  2111.                     PATFETCH (c1);
  2112.                     
  2113.                     ret = compile_range (rxb, cs, &p, pend, translate, syntax,
  2114.                      inverse_translate, validate_inv_tr);
  2115.                     if (ret != REG_NOERROR) return ret;
  2116.                   }
  2117.         
  2118.                 /* See if we're at the beginning of a possible character
  2119.                    class.  */
  2120.         
  2121.         else if ((syntax & RE_CHAR_CLASSES)
  2122.              && (c == '[') && (*p == ':'))
  2123.                   {
  2124.                     char str[CHAR_CLASS_MAX_LENGTH + 1];
  2125.             
  2126.                     PATFETCH (c);
  2127.                     c1 = 0;
  2128.             
  2129.                     /* If pattern is `[[:'.  */
  2130.                     if (p == pend) return REG_EBRACK;
  2131.             
  2132.                     for (;;)
  2133.                       {
  2134.                         PATFETCH (c);
  2135.                         if (c == ':' || c == ']' || p == pend
  2136.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  2137.               break;
  2138.                         str[c1++] = c;
  2139.                       }
  2140.                     str[c1] = '\0';
  2141.             
  2142.                     /* If isn't a word bracketed by `[:' and:`]':
  2143.                        undo the ending character, the letters, and leave 
  2144.                        the leading `:' and `[' (but set bits for them).  */
  2145.                     if (c == ':' && *p == ']')
  2146.                       {
  2147.                         int ch;
  2148.                         boolean is_alnum = !strcmp (str, "alnum");
  2149.                         boolean is_alpha = !strcmp (str, "alpha");
  2150.                         boolean is_blank = !strcmp (str, "blank");
  2151.                         boolean is_cntrl = !strcmp (str, "cntrl");
  2152.                         boolean is_digit = !strcmp (str, "digit");
  2153.                         boolean is_graph = !strcmp (str, "graph");
  2154.                         boolean is_lower = !strcmp (str, "lower");
  2155.                         boolean is_print = !strcmp (str, "print");
  2156.                         boolean is_punct = !strcmp (str, "punct");
  2157.                         boolean is_space = !strcmp (str, "space");
  2158.                         boolean is_upper = !strcmp (str, "upper");
  2159.                         boolean is_xdigit = !strcmp (str, "xdigit");
  2160.                         
  2161.                         if (!IS_CHAR_CLASS (str)) return REG_ECTYPE;
  2162.             
  2163.                         /* Throw away the ] at the end of the character
  2164.                            class.  */
  2165.                         PATFETCH (c);                    
  2166.             
  2167.                         if (p == pend) return REG_EBRACK;
  2168.             
  2169.                         for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  2170.                           {
  2171.                             if (   (is_alnum  && isalnum (ch))
  2172.                                 || (is_alpha  && isalpha (ch))
  2173.                                 || (is_blank  && isblank (ch))
  2174.                                 || (is_cntrl  && iscntrl (ch))
  2175.                                 || (is_digit  && isdigit (ch))
  2176.                                 || (is_graph  && isgraph (ch))
  2177.                                 || (is_lower  && islower (ch))
  2178.                                 || (is_print  && isprint (ch))
  2179.                                 || (is_punct  && ispunct (ch))
  2180.                                 || (is_space  && isspace (ch))
  2181.                                 || (is_upper  && isupper (ch))
  2182.                                 || (is_xdigit && isxdigit (ch)))
  2183.                   {
  2184.                 rx_Bitset it =
  2185.                   inverse_translation (rxb, 
  2186.                                validate_inv_tr,
  2187.                                inverse_translate,
  2188.                                translate,
  2189.                                ch);
  2190.                 rx_bitset_union (rxb->rx.local_cset_size,
  2191.                          cs, it);
  2192.                   }
  2193.                           }
  2194.                         had_char_class = true;
  2195.                       }
  2196.                     else
  2197.                       {
  2198.                         c1++;
  2199.                         while (c1--)    
  2200.                           PATUNFETCH;
  2201.             {
  2202.               rx_Bitset it =
  2203.                 inverse_translation (rxb, 
  2204.                          validate_inv_tr,
  2205.                          inverse_translate,
  2206.                          translate,
  2207.                          '[');
  2208.               rx_bitset_union (rxb->rx.local_cset_size,
  2209.                        cs, it);
  2210.             }
  2211.             {
  2212.               rx_Bitset it =
  2213.                 inverse_translation (rxb, 
  2214.                          validate_inv_tr,
  2215.                          inverse_translate,
  2216.                          translate,
  2217.                          ':');
  2218.               rx_bitset_union (rxb->rx.local_cset_size,
  2219.                        cs, it);
  2220.             }
  2221.                         had_char_class = false;
  2222.                       }
  2223.                   }
  2224.                 else
  2225.                   {
  2226.                     had_char_class = false;
  2227.             {
  2228.               rx_Bitset it = inverse_translation (rxb, 
  2229.                               validate_inv_tr,
  2230.                               inverse_translate,
  2231.                               translate,
  2232.                               c);
  2233.               rx_bitset_union (rxb->rx.local_cset_size, cs, it);
  2234.             }
  2235.                   }
  2236.               }
  2237.  
  2238.       finalize_class_and_append:
  2239.         if (is_inverted)
  2240.           {
  2241.         rx_bitset_complement (rxb->rx.local_cset_size, cs);
  2242.         if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
  2243.           RX_bitset_remove (cs, '\n');
  2244.           }
  2245.         goto append_node;
  2246.           }
  2247.           break;
  2248.  
  2249.  
  2250.     case '(':
  2251.           if (syntax & RE_NO_BK_PARENS)
  2252.             goto handle_open;
  2253.           else
  2254.             goto normal_char;
  2255.  
  2256.  
  2257.         case ')':
  2258.           if (syntax & RE_NO_BK_PARENS)
  2259.             goto handle_close;
  2260.           else
  2261.             goto normal_char;
  2262.  
  2263.  
  2264.         case '\n':
  2265.           if (syntax & RE_NEWLINE_ALT)
  2266.             goto handle_alt;
  2267.           else
  2268.             goto normal_char;
  2269.  
  2270.  
  2271.     case '|':
  2272.           if (syntax & RE_NO_BK_VBAR)
  2273.             goto handle_alt;
  2274.           else
  2275.             goto normal_char;
  2276.  
  2277.  
  2278.         case '{':
  2279.       if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  2280.         goto handle_interval;
  2281.       else
  2282.         goto normal_char;
  2283.  
  2284.  
  2285.         case '\\':
  2286.           if (p == pend) return REG_EESCAPE;
  2287.  
  2288.           /* Do not translate the character after the \, so that we can
  2289.              distinguish, e.g., \B from \b, even if we normally would
  2290.              translate, e.g., B to b.  */
  2291.           PATFETCH_RAW (c);
  2292.  
  2293.           switch (c)
  2294.             {
  2295.             case '(':
  2296.               if (syntax & RE_NO_BK_PARENS)
  2297.                 goto normal_backslash;
  2298.  
  2299.             handle_open:
  2300.               rxb->re_nsub++;
  2301.               regnum++;
  2302.               if (COMPILE_STACK_FULL)
  2303.                 { 
  2304.                   RETALLOC (compile_stack.stack, compile_stack.size << 1,
  2305.                             compile_stack_elt_t);
  2306.                   if (compile_stack.stack == 0) return REG_ESPACE;
  2307.  
  2308.                   compile_stack.size <<= 1;
  2309.                 }
  2310.  
  2311.           if (*last_expression)
  2312.         {
  2313.           struct rexp_node * concat
  2314.             = rx_mk_r_concat (&rxb->rx, *last_expression, 0);
  2315.           if (!concat)
  2316.             return REG_ESPACE;
  2317.           *last_expression = concat;
  2318.           last_expression = &concat->params.pair.right;
  2319.         }
  2320.  
  2321.               /*
  2322.            * These are the values to restore when we hit end of this
  2323.                * group.  
  2324.            */
  2325.           COMPILE_STACK_TOP.top_expression = top_expression;
  2326.           COMPILE_STACK_TOP.last_expression = last_expression;
  2327.               COMPILE_STACK_TOP.regnum = regnum;
  2328.           
  2329.               compile_stack.avail++;
  2330.           
  2331.           top_expression = last_expression;
  2332.           break;
  2333.  
  2334.  
  2335.             case ')':
  2336.               if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  2337.  
  2338.             handle_close:
  2339.               /* See similar code for backslashed left paren above.  */
  2340.               if (COMPILE_STACK_EMPTY)
  2341.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2342.                   goto normal_char;
  2343.                 else
  2344.                   return REG_ERPAREN;
  2345.  
  2346.               /* Since we just checked for an empty stack above, this
  2347.                  ``can't happen''.  */
  2348.  
  2349.               {
  2350.                 /* We don't just want to restore into `regnum', because
  2351.                    later groups should continue to be numbered higher,
  2352.                    as in `(ab)c(de)' -- the second group is #2.  */
  2353.                 regnum_t this_group_regnum;
  2354.         struct rexp_node ** inner = top_expression;
  2355.  
  2356.                 compile_stack.avail--;
  2357.         top_expression = COMPILE_STACK_TOP.top_expression;
  2358.         last_expression = COMPILE_STACK_TOP.last_expression;
  2359.                 this_group_regnum = COMPILE_STACK_TOP.regnum;
  2360.         {
  2361.           int left_se = paramc;
  2362.           int right_se = paramc + 1;
  2363.  
  2364.           params = (params
  2365.                 ? ((struct re_se_params *)
  2366.                    realloc (params,
  2367.                     (paramc + 2) * sizeof (params[0])))
  2368.                 : ((struct re_se_params *)
  2369.                    malloc (2 * sizeof (params[0]))));
  2370.           if (!params)
  2371.             return REG_ESPACE;
  2372.           paramc += 2;
  2373.  
  2374.           params[left_se].se = re_se_lparen;
  2375.           params[left_se].op1 = this_group_regnum;
  2376.           params[right_se].se = re_se_rparen;
  2377.           params[right_se].op1 = this_group_regnum;
  2378.           {
  2379.             struct rexp_node * left
  2380.               = rx_mk_r_side_effect (&rxb->rx,
  2381.                          (rx_side_effect)left_se);
  2382.             struct rexp_node * right
  2383.               = rx_mk_r_side_effect (&rxb->rx,
  2384.                          (rx_side_effect)right_se);
  2385.             struct rexp_node * c1
  2386.               = (*inner
  2387.              ? rx_mk_r_concat (&rxb->rx, left, *inner) : left);
  2388.             struct rexp_node * c2
  2389.               = rx_mk_r_concat (&rxb->rx, c1, right);
  2390.             if (!(left && right && c1 && c2))
  2391.               return REG_ESPACE;
  2392.             *inner = c2;
  2393.           }
  2394.         }
  2395.         break;
  2396.           }
  2397.  
  2398.             case '|':                    /* `\|'.  */
  2399.               if ((syntax & RE_LIMITED_OPS) || (syntax & RE_NO_BK_VBAR))
  2400.                 goto normal_backslash;
  2401.             handle_alt:
  2402.               if (syntax & RE_LIMITED_OPS)
  2403.                 goto normal_char;
  2404.  
  2405.           {
  2406.         struct rexp_node * alt
  2407.           = rx_mk_r_alternate (&rxb->rx, *top_expression, 0);
  2408.         if (!alt)
  2409.           return REG_ESPACE;
  2410.         *top_expression = alt;
  2411.         last_expression = &alt->params.pair.right;
  2412.         {
  2413.           int sync_se = paramc;
  2414.  
  2415.           params = (params
  2416.                 ? ((struct re_se_params *)
  2417.                    realloc (params,
  2418.                     (paramc + 1) * sizeof (params[0])))
  2419.                 : ((struct re_se_params *)
  2420.                    malloc (sizeof (params[0]))));
  2421.           if (!params)
  2422.             return REG_ESPACE;
  2423.           ++paramc;
  2424.  
  2425.           params[sync_se].se = re_se_tv;
  2426.           {
  2427.             struct rexp_node * sync
  2428.               = rx_mk_r_side_effect (&rxb->rx,
  2429.                          (rx_side_effect)sync_se);
  2430.             struct rexp_node * conc
  2431.               = rx_mk_r_concat (&rxb->rx, sync, 0);
  2432.  
  2433.             if (!sync || !conc)
  2434.               return REG_ESPACE;
  2435.  
  2436.             *last_expression = conc;
  2437.             last_expression = &conc->params.pair.right;
  2438.           }
  2439.         }
  2440.           }
  2441.               break;
  2442.  
  2443.  
  2444.             case '{': 
  2445.               /* If \{ is a literal.  */
  2446.               if (!(syntax & RE_INTERVALS)
  2447.                      /* If we're at `\{' and it's not the open-interval 
  2448.                         operator.  */
  2449.                   || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  2450.                   || (p - 2 == pattern  &&  p == pend))
  2451.                 goto normal_backslash;
  2452.  
  2453.             handle_interval:
  2454.               {
  2455.                 /* If got here, then the syntax allows intervals.  */
  2456.  
  2457.                 /* At least (most) this many matches must be made.  */
  2458.                 int lower_bound = -1, upper_bound = -1;
  2459.  
  2460.                 beg_interval = p - 1;
  2461.  
  2462.                 if (p == pend)
  2463.                   {
  2464.                     if (syntax & RE_NO_BK_BRACES)
  2465.                       goto unfetch_interval;
  2466.                     else
  2467.                       return REG_EBRACE;
  2468.                   }
  2469.  
  2470.                 GET_UNSIGNED_NUMBER (lower_bound);
  2471.  
  2472.                 if (c == ',')
  2473.                   {
  2474.                     GET_UNSIGNED_NUMBER (upper_bound);
  2475.                     if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  2476.                   }
  2477.                 else
  2478.                   /* Interval such as `{1}' => match exactly once. */
  2479.                   upper_bound = lower_bound;
  2480.  
  2481.                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  2482.                     || lower_bound > upper_bound)
  2483.                   {
  2484.                     if (syntax & RE_NO_BK_BRACES)
  2485.                       goto unfetch_interval;
  2486.                     else 
  2487.                       return REG_BADBR;
  2488.                   }
  2489.  
  2490.                 if (!(syntax & RE_NO_BK_BRACES)) 
  2491.                   {
  2492.                     if (c != '\\') return REG_EBRACE;
  2493.                     PATFETCH (c);
  2494.                   }
  2495.  
  2496.                 if (c != '}')
  2497.                   {
  2498.                     if (syntax & RE_NO_BK_BRACES)
  2499.                       goto unfetch_interval;
  2500.                     else 
  2501.                       return REG_BADBR;
  2502.                   }
  2503.  
  2504.                 /* We just parsed a valid interval.  */
  2505.  
  2506.                 /* If it's invalid to have no preceding re.  */
  2507.                 if (pointless_if_repeated (*last_expression, params))
  2508.                   {
  2509.                     if (syntax & RE_CONTEXT_INVALID_OPS)
  2510.                       return REG_BADRPT;
  2511.                     else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  2512.                       goto unfetch_interval;
  2513.             /* was: else laststart = b; */
  2514.                   }
  2515.  
  2516.                 /* If the upper bound is zero, don't want to iterate
  2517.                  * at all.
  2518.          */
  2519.                  if (upper_bound == 0)
  2520.            {
  2521.              if (*last_expression)
  2522.                {
  2523.              rx_free_rexp (&rxb->rx, *last_expression);
  2524.              *last_expression = 0;
  2525.                }
  2526.            }
  2527.         else
  2528.           /* Otherwise, we have a nontrivial interval. */
  2529.           {
  2530.             int iter_se = paramc;
  2531.             int end_se = paramc + 1;
  2532.             params = (params
  2533.                   ? ((struct re_se_params *)
  2534.                  realloc (params,
  2535.                       sizeof (*params) * (2 + paramc)))
  2536.                   : ((struct re_se_params *)
  2537.                  malloc (2 * sizeof (*params))));
  2538.             if (!params)
  2539.               return REG_ESPACE;
  2540.             paramc += 2;
  2541.             params [iter_se].se = re_se_iter;
  2542.             params [iter_se].op1 = lower_bound;
  2543.             params[iter_se].op2 = upper_bound;
  2544.  
  2545.             params[end_se].se = re_se_end_iter;
  2546.             params[end_se].op1 = lower_bound;
  2547.             params[end_se].op2 = upper_bound;
  2548.             {
  2549.               struct rexp_node * push0
  2550.             = rx_mk_r_side_effect (&rxb->rx,
  2551.                            (rx_side_effect)re_se_push0);
  2552.               struct rexp_node * start_one_iter
  2553.             = rx_mk_r_side_effect (&rxb->rx,
  2554.                            (rx_side_effect)iter_se);
  2555.               struct rexp_node * phase1
  2556.             = rx_mk_r_concat (&rxb->rx, start_one_iter,
  2557.                       *last_expression);
  2558.               struct rexp_node * pushback
  2559.             = rx_mk_r_side_effect (&rxb->rx,
  2560.                            (rx_side_effect)re_se_pushback);
  2561.               rx_Bitset cs = rx_cset (&rxb->rx);
  2562.               struct rexp_node * lit_t
  2563.             = rx_mk_r_cset (&rxb->rx, cs);
  2564.               struct rexp_node * phase2
  2565.             = rx_mk_r_concat (&rxb->rx, pushback, lit_t);
  2566.               struct rexp_node * loop
  2567.             = rx_mk_r_2phase_star (&rxb->rx, phase1, phase2);
  2568.               struct rexp_node * push_n_loop
  2569.             = rx_mk_r_concat (&rxb->rx, push0, loop);
  2570.               struct rexp_node * final_test
  2571.             = rx_mk_r_side_effect (&rxb->rx,
  2572.                            (rx_side_effect)end_se);
  2573.               struct rexp_node * full_exp
  2574.             = rx_mk_r_concat (&rxb->rx, push_n_loop, final_test);
  2575.  
  2576.               if (!(push0 && start_one_iter && phase1
  2577.                 && pushback && lit_t && phase2
  2578.                 && loop && push_n_loop && final_test && full_exp))
  2579.             return REG_ESPACE;
  2580.  
  2581.               RX_bitset_enjoin(cs, 't');
  2582.  
  2583.               *last_expression = full_exp;
  2584.             }
  2585.           }
  2586.                 beg_interval = 0;
  2587.               }
  2588.               break;
  2589.  
  2590.             unfetch_interval:
  2591.               /* If an invalid interval, match the characters as literals.  */
  2592.                p = beg_interval;
  2593.                beg_interval = NULL;
  2594.  
  2595.                /* normal_char and normal_backslash need `c'.  */
  2596.                PATFETCH (c);    
  2597.  
  2598.                if (!(syntax & RE_NO_BK_BRACES))
  2599.   c'.  */
  2600.     normal_char:
  2601.         {
  2602.           rx_Bitset cs = rx_cset(&rxb->rx);
  2603.           struct rexp_node * match = rx_mk_r_cset (&rxb->rx, cs);
  2604.           rx_Bitset it;
  2605.           if (!(cs && match))
  2606.         return REG_ESPACE;
  2607.           it = inverse_translation (rxb, validate_inv_tr,
  2608.                     inverse_translate, translate, c);
  2609.           rx_bitset_union (CHAR_SET_SIZE, cs, it);
  2610.           append = match;
  2611.  
  2612.         append_node:
  2613.           /* This genericly appends the rexp APPEND to *LAST_EXPRESSION
  2614.            * and then parses the next character normally.
  2615.            */
  2616.           if (*last_expression)
  2617.         {
  2618.           struct rexp_node * concat
  2619.             = rx_mk_r_concat (&rxb->rx, *last_expression, append);
  2620.           if (!concat)
  2621.             return REG_ESPACE;
  2622.           *last_expression = concat;
  2623.           last_expression = &concat->params.pair.right;
  2624.         }
  2625.           else
  2626.         *last_expression = append;
  2627.         }
  2628.     } /* switch (c) */
  2629.     } /* while p != pend */
  2630.  
  2631.   
  2632.   {
  2633.     int win_se = paramc;
  2634.     params = (params
  2635.           ? ((struct re_se_params *)
  2636.          realloc (params,
  2637.               sizeof (*params) * (1 + paramc)))
  2638.           : ((struct re_se_params *)
  2639.          malloc (sizeof (*params))));
  2640.     if (!params)
  2641.       return REG_ESPACE;
  2642.     ++paramc;
  2643.     params[win_se].se = re_se_win;
  2644.     {
  2645.       struct rexp_node * se
  2646.     = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)win_se);
  2647.       struct rexp_node * concat
  2648.     = rx_mk_r_concat (&rxb->rx, rexp, se);
  2649.       if (!(se && concat))
  2650.     return REG_ESPACE;
  2651.       rexp = concat;
  2652.     }
  2653.   }
  2654.  
  2655.  
  2656.   /* Through the pattern now.  */
  2657.  
  2658.   if (!COMPILE_STACK_EMPTY) 
  2659.     return REG_EPAREN;
  2660.  
  2661.       free (compile_stack.stack);
  2662.  
  2663.   orig_rexp = rexp;
  2664. #ifdef RX_DEBUG
  2665.   if (rx_debug_compile)
  2666.     {
  2667.       dbug_rxb = rxb;
  2668.       fputs ("\n\nCompiling ", stdout);
  2669.       fwrite (pattern, 1, size, stdout);
  2670.       fputs (":\n", stdout);
  2671.       rxb->se_params = params;
  2672.       print_rexp (&rxb->rx, orig_rexp, 2, re_seprint, stdout);
  2673.     }
  2674. #endif
  2675.   {
  2676.     rx_Bitset cs = rx_cset(&rxb->rx);
  2677.     rx_Bitset cs2 = rx_cset(&rxb->rx);
  2678.     char * se_map = (char *) alloca (paramc);
  2679.     struct rexp_node * new_rexp = 0;
  2680.  
  2681.  
  2682.     bzero (se_map, paramc);
  2683.     find_backrefs (se_map, rexp, params);
  2684.     fewer_side_effects =
  2685.       remove_unecessary_side_effects (&rxb->rx, se_map,
  2686.                       rx_copy_rexp (&rxb->rx, rexp), params);
  2687.  
  2688.     speed_up_alt (&rxb->rx, rexp, 0);
  2689.     speed_up_alt (&rxb->rx, fewer_side_effects, 1);
  2690.  
  2691.     {
  2692.       char * syntax_parens = rxb->syntax_parens;
  2693.       if (syntax_parens == (char *)0x1)
  2694.     rexp = remove_unecessary_side_effects
  2695.       (&rxb->rx, se_map, rexp, params);
  2696.       else if (syntax_parens)
  2697.     {
  2698.       int x;
  2699.       for (x = 0; x < paramc; ++x)
  2700.         if ((   (params[x].se == re_se_lparen)
  2701.          || (params[x].se == re_se_rparen))
  2702.         && (!syntax_parens [params[x].op1]))
  2703.           se_map [x] = 1;
  2704.       rexp = remove_unecessary_side_effects
  2705.         (&rxb->rx, se_map, rexp, params);
  2706.     }
  2707.     }
  2708.  
  2709.     /* At least one more optimization would be nice to have here but i ran out 
  2710.      * of time.  The idea would be to delay side effects.  
  2711.      * For examle, `(abc)' is the same thing as `abc()' except that the
  2712.      * left paren is offset by 3 (which we know at compile time).
  2713.      * (In this comment, write that second pattern `abc(:3:)' 
  2714.      * where `(:3:' is a syntactic unit.)
  2715.      *
  2716.      * Trickier:  `(abc|defg)'  is the same as `(abc(:3:|defg(:4:))'
  2717.      * (The paren nesting may be hard to follow -- that's an alternation
  2718.      *    of `abc(:3:' and `defg(:4:' inside (purely syntactic) parens
  2719.      *  followed by the closing paren from the original expression.)
  2720.      *
  2721.      * Neither the expression tree representation nor the the nfa make
  2722.      * this very easy to write. :(
  2723.      */
  2724.  
  2725.   /* What we compile is different than what the parser returns.
  2726.    * Suppose the parser returns expression R.
  2727.    * Let R' be R with unnecessary register assignments removed 
  2728.    * (see REMOVE_UNECESSARY_SIDE_EFFECTS, above).
  2729.    *
  2730.    * What we will compile is the expression:
  2731.    *
  2732.    *    m{try}R{win}\|s{try}R'{win}
  2733.    *
  2734.    * {try} and {win} denote side effect epsilons (see EXPLORE_FUTURE).
  2735.    * 
  2736.    * When trying a match, we insert an `m' at the beginning of the 
  2737.    * string if the user wants registers to be filled, `s' if not.
  2738.    */
  2739.     new_rexp =
  2740.       rx_mk_r_alternate
  2741.     (&rxb->rx,
  2742.      rx_mk_r_concat (&rxb->rx, rx_mk_r_cset (&rxb->rx, cs2), rexp),
  2743.      rx_mk_r_concat (&rxb->rx,
  2744.              rx_mk_r_cset (&rxb->rx, cs), fewer_side_effects));
  2745.  
  2746.     if (!(new_rexp && cs && cs2))
  2747.       return REG_ESPACE;
  2748.     RX_bitset_enjoin (cs2, '\0'); /* prefixed to the rexp used for matching. */
  2749.     RX_bitset_enjoin (cs, '\1'); /* prefixed to the rexp used for searching. */
  2750.     rexp = new_rexp;
  2751.   }
  2752.  
  2753. #ifdef RX_DEBUG
  2754.   if (rx_debug_compile)
  2755.     {
  2756.       fputs ("\n...which is compiled as:\n", stdout);
  2757.       print_rexp (&rxb->rx, rexp, 2, re_seprint, stdout);
  2758.     }
  2759. #endif
  2760.   {
  2761.     struct rx_nfa_state *start = 0;
  2762.     struct rx_nfa_state *end = 0;
  2763.  
  2764.     if (!rx_build_nfa (&rxb->rx, rexp, &start, &end))
  2765.       return REG_ESPACE;    /*  */
  2766.     else
  2767.       {
  2768.     void * mem = (void *)rxb->buffer;
  2769.     unsigned long size = rxb->allocated;
  2770.     int start_id;
  2771.     char * perm_mem;
  2772.     int iterator_size = paramc * sizeof (params[0]);
  2773.  
  2774.     end->is_final = 1;
  2775.     start->is_start = 1;
  2776.     rx_name_nfa_states (&rxb->rx);
  2777.     start_id = start->id;
  2778. #ifdef RX_DEBUG
  2779.     if (rx_debug_compile)
  2780.       {
  2781.         fputs ("...giving the NFA: \n", stdout);
  2782.         dbug_rxb = rxb;
  2783.         print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  2784.       }
  2785. #endif
  2786.     if (!rx_eclose_nfa (&rxb->rx))
  2787.       return REG_ESPACE;
  2788.     else
  2789.       {
  2790.         rx_delete_epsilon_transitions (&rxb->rx);
  2791.         
  2792.         /* For compatability reasons, we need to shove the
  2793.          * compiled nfa into one chunk of malloced memory.
  2794.          */
  2795.         rxb->rx.reserved = (   sizeof (params[0]) * paramc
  2796.                 +  rx_sizeof_bitset (rxb->rx.local_cset_size));
  2797. #ifdef RX_DEBUG
  2798.         if (rx_debug_compile)
  2799.           {
  2800.         dbug_rxb = rxb;
  2801.         fputs ("...which cooks down (uncompactified) to: \n", stdout);
  2802.         print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  2803.           }
  2804. #endif
  2805.         if (!rx_compactify_nfa (&rxb->rx, &mem, &size))
  2806.           return REG_ESPACE;
  2807.         rxb->buffer = mem;
  2808.         rxb->allocated = size;
  2809.         rxb->rx.buffer = mem;
  2810.         rxb->rx.allocated = size;
  2811.         perm_mem = ((char *)rxb->rx.buffer
  2812.             + rxb->rx.allocated - rxb->rx.reserved);
  2813.         rxb->se_params = ((struct re_se_params *)perm_mem);
  2814.         bcopy (params, rxb->se_params, iterator_size);
  2815.         perm_mem += iterator_size;
  2816.         rxb->fastset = (rx_Bitset) perm_mem;
  2817.         rxb->start = rx_id_to_nfa_state (&rxb->rx, start_id);
  2818.       }
  2819.     rx_bitset_null (rxb->rx.local_cset_size, rxb->fastset);
  2820.     rxb->can_match_empty = compute_fastset (rxb, orig_rexp);
  2821.     rxb->match_regs_on_stack =
  2822.       registers_on_stack (rxb, orig_rexp, 0, params); 
  2823.     rxb->search_regs_on_stack =
  2824.       registers_on_stack (rxb, fewer_side_effects, 0, params);
  2825.     if (rxb->can_match_empty)
  2826.       rx_bitset_universe (rxb->rx.local_cset_size, rxb->fastset);
  2827.     rxb->is_anchored = is_anchored (orig_rexp, (rx_side_effect) re_se_hat);
  2828.     rxb->begbuf_only = is_anchored (orig_rexp,
  2829.                     (rx_side_effect) re_se_begbuf);
  2830.       }
  2831.     rx_free_rexp (&rxb->rx, rexp);
  2832.     if (params)
  2833.       free (params);
  2834. #ifdef RX_DEBUG
  2835.     if (rx_debug_compile)
  2836.       {
  2837.     dbug_rxb = rxb;
  2838.     fputs ("...which cooks down to: \n", stdout);
  2839.     print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  2840.       }
  2841. #endif
  2842.   }
  2843.   return REG_NOERROR;
  2844. }
  2845.  
  2846.  
  2847.  
  2848. /* This table gives an error message for each of the error codes listed
  2849.    in regex.h.  Obviously the order here has to be same as there.  */
  2850.  
  2851. const char * rx_error_msg[] =
  2852. { 0,                        /* REG_NOERROR */
  2853.     "No match",                    /* REG_NOMATCH */
  2854.     "Invalid regular expression",        /* REG_BADPAT */
  2855.     "Invalid collation character",        /* REG_ECOLLATE */
  2856.     "Invalid character class name",        /* REG_ECTYPE */
  2857.     "Trailing backslash",            /* REG_EESCAPE */
  2858.     "Invalid back reference",            /* REG_ESUBREG */
  2859.     "Unmatched [ or [^",            /* REG_EBRACK */
  2860.     "Unmatched ( or \\(",            /* REG_EPAREN */
  2861.     "Unmatched \\{",                /* REG_EBRACE */
  2862.     "Invalid content of \\{\\}",        /* REG_BADBR */
  2863.     "Invalid range end",            /* REG_ERANGE */
  2864.     "Memory exhausted",                /* REG_ESPACE */
  2865.     "Invalid preceding regular expression",    /* REG_BADRPT */
  2866.     "Premature end of regular expression",    /* REG_EEND */
  2867.     "Regular expression too big",        /* REG_ESI_t);
  2868.       if (regs->start == 0 || regs->end == 0)
  2869.         return -2;
  2870.       rxb->regs_allocated = REGS_REALLOCATE;
  2871.     }
  2872.       else if (rxb->regs_allocated == REGS_REALLOCATE)
  2873.     { /* Yes.  If we need more elements than were already
  2874.          allocated, reallocate them.  If we need fewer, just
  2875.          leave it alone.  */
  2876.       if (regs->num_regs < num_regs + 1)
  2877.         {
  2878.           regs->num_regs = num_regs + 1;
  2879.           RETALLOC (regs->start, regs->num_regs, regoff_t);
  2880.           RETALLOC (regs->end, regs->num_regs, regoff_t);
  2881.           if (regs->start == 0 || regs->end == 0)
  2882.         return -2;
  2883.         }
  2884.     }
  2885.       else if (rxb->regs_allocated != REGS_FIXED)
  2886.     return -2;
  2887.  
  2888.       if (regs->num_regs < num_regs + 1)
  2889.     {
  2890.       best_lpspace = ((regoff_t *)
  2891.               REGEX_ALLOCATE (num_regs * sizeof(regoff_t)));
  2892.       best_rpspace = ((regoff_t *)
  2893.               REGEX_ALLOCATE (num_regs * sizeof(regoff_t)));
  2894.       best_lparen = best_lpspace;
  2895.       best_rparen = best_rpspace;
  2896.     }
  2897.       else
  2898.     {
  2899.       best_lparen = regs->start;
  2900.       best_rparen = regs->end;
  2901.     }
  2902.     }
  2903.   
  2904.   lparen = (regoff_t *) REGEX_ALLOCATE (num_regs * sizeof(regoff_t));
  2905.   rparen = (regoff_t *) REGEX_ALLOCATE (num_regs * sizeof(regoff_t)); 
  2906.   
  2907.   if (!(best_rparen && best_lparen && lparen && rparen))
  2908.     return -2;
  2909.   
  2910.   best_last_l = best_last_r = -1;
  2911.  
  2912.  
  2913.  
  2914.   /***** fastmap/search loop, initialization */
  2915.  
  2916.   /* This is the loop that scans using the fastmap, and sometimes tries to 
  2917.    * match. From this point on, don't return.  Instead, assign to ret_val
  2918.    * and goto fail.
  2919.    */
  2920.   {
  2921.     const char * translate = rxb->translate ? rxb->translate : id_translation;
  2922.  
  2923.     /** This is state associated with returning to the caller. */
  2924.  
  2925.     int ret_val = -1;
  2926.  
  2927.     /*   A sentinal is sometimes installed in the fastmap.  This records
  2928.      *   where so it can be removed before returning.
  2929.      */
  2930.     int fastmap_chr = -1;
  2931.     int fastmap_val;
  2932.  
  2933.     /** End of state associated with returning to the caller. */
  2934.  
  2935.     /** Start of variables associated with the fastmap based search: */
  2936.  
  2937.     char * fastmap = rxb->fastmap ? (char *)rxb->fastmap : (char *)slowmap;
  2938.     int search_direction;    /* 1 or -1 */
  2939.     int search_end;        /* first position to not try */
  2940.     int offset;            /* either size1 or 0 as string == string2 */
  2941.  
  2942.     /* The string-pair position of the fastmap/search loop: */
  2943.     const unsigned char * pos;    /* The current pos. */
  2944.     const unsigned char * string; /* The current string half. */
  2945.     const unsigned char * end;    /* End of current string. */
  2946.     int size;            /* Current string's size */
  2947.     int half;            /* 0 means string1, 1 means string2 */
  2948.  
  2949.     /** End of variables associated with the fastmap based search: */
  2950.  
  2951.  
  2952.     /** Start of variables associated with trying a match
  2953.      *  after the fastmap has found a plausible starting point.
  2954.      */
  2955.  
  2956.     struct rx_superstate * start_super = 0; /* The superNFA start state. */
  2957.  
  2958.     /*
  2959.      * Two nfa's were compiled.  
  2960.      * `0' is complete.
  2961.      * `1' faster but gets registers wrong and ends too soon.
  2962.      */
  2963.     int nfa_choice = ((regs && !rxb->least_subs) ? '\0' : '\1');
  2964.  
  2965.     const unsigned char * abs_end; /* Don't fetch a character from here. */
  2966.     int first_found;        /* If true, return after finding any match. */
  2967.  
  2968.     /** End of variables associated with trying a match. */
  2969.  
  2970.     /* Update the fastmap now if not correct already. 
  2971.      * When the regexp was compiled, the fastmap was computed
  2972.      * and stored in a bitset.  This expands the bitset into a
  2973.      * character array containing 1s and 0s.
  2974.      */
  2975.     if ((fastmap == rxb->fastmap) && !rxb->fastmap_accurate)
  2976.       rx_blow_up_fastmap (rxb);
  2977.  
  2978.     /* Now we build the starting state of the supernfa. */
  2979.     {
  2980.       struct rx_superset * start_contents;
  2981.       struct rx_nfa_state_set * start_nfa_set;
  2982.       
  2983.       /* We presume here that the nfa start state has only one
  2984.        * possible future with no side effects.  
  2985.        */
  2986.       start_nfa_set = rxb->start->futures->destset;
  2987.       if (   rxb->rx.start_set
  2988.       && (rxb->rx.start_set->starts_for == &rxb->rx))
  2989.     start_contents = rxb->rx.start_set;
  2990.       else
  2991.     {
  2992.       start_contents =
  2993.         rx_superstate_eclosure_union (&rxb->rx,
  2994.                       rx_superset_cons (&rxb->rx, 0, 0),
  2995.                       start_nfa_set);
  2996.       
  2997.       if (!start_contents)
  2998.         return -1;
  2999.  
  3000.       start_contents->starts_for = &rxb->rx;
  3001.       rxb->rx.start_set = start_contents;
  3002.     }
  3003.       if (   start_contents->superstate
  3004.       && (start_contents->superstate->rx_id == rxb->rx.rx_id))
  3005.     {
  3006.       start_super = start_contents->superstate;
  3007.       rx_lock_superstate (&rxb->rx, start_super);
  3008.     }
  3009.       else
  3010.     {
  3011.       rx_protect_superset (&rxb->rx, start_contents);
  3012.       
  3013.       start_super = rx_superstate (&rxb->rx, start_contents);
  3014.       if (!start_super)
  3015.         return -1;
  3016.       rx_lock_superstate (&rxb->rx, start_super);
  3017.       rx_release_superset (&rxb->rx, start_contents);
  3018.     }
  3019.     }
  3020.     
  3021.     /* This computes an upper bound on string addresses for use by
  3022.      * the match-test.
  3023.      */
  3024.     abs_end = ((const unsigned char *) ((stop <= size1)
  3025.                     ? string1 + stop
  3026.                     : string2 + stop - size1));
  3027.  
  3028.     /* We have the option to look for the best match or the first
  3029.      * one we can find.  If the user isn't asking for register information,
  3030.      * we don't need to find the best match.
  3031.      */
  3032.     first_found = !regs;
  3033.  
  3034.     /* Compute search_end & search_direction for the fastmap loop. */
  3035.     if (range >= 0)
  3036.       {
  3037.     search_end = MIN (size1 + size2, startpos + range) + 1;
  3038.     search_direction = 1;
  3039.       }
  3040.     else
  3041.       {
  3042.     search_end = MAX(-1, startpos + range);
  3043.     search_direction = -1;
  3044.       }
  3045.  
  3046.     /* The vacuous search always turns up nothing. */
  3047.     if ((search_direction == 1)
  3048.     ? (startpos > search_end)
  3049.     : (startpos < search_end))
  3050.       return -1;
  3051.  
  3052.     /* Set string/size/offset/end -- the state that tells the fastmap
  3053.      * loop which half of the string we're in.  Also set pos, which
  3054.      * is the addr of the current fastmap scan position.
  3055.      */
  3056.     if (!string2 || (startpos < size1))
  3057.       {
  3058.     string = (const unsigned char *)string1;
  3059.     size = size1;
  3060.     offset = 0;
  3061.     pos = (const unsigned char *)(string1 + startpos);
  3062.     half = 0;
  3063.     end = (const unsigned char *)MIN(string1 + size1, string1 + stop);
  3064.       }
  3065.     else
  3066.       {
  3067.     string = (const unsigned char *)string2;
  3068.     size = size2;
  3069.     offset = size1;
  3070.     pos = (const unsigned char *)(string2 + startpos - size1);
  3071.     half = 1;
  3072.     end = (const unsigned char *)MIN(string2 + size2,
  3073.                      string2 + stop - size1);
  3074.       }
  3075.  
  3076.  
  3077.  
  3078.  
  3079.     /***** fastmap/search loop,  body */
  3080.  
  3081.  
  3082.   init_fastmap_sentinal:
  3083.  
  3084.     /* For the sake of fast fastmapping, set a sentinal in the fastmap.
  3085.      * This sentinal will trap the fastmap loop when it reaches the last
  3086.      * valid character in a string half.
  3087.      *
  3088.      * This must be reset when the fastmap/search loop crosses a string 
  3089.      * boundry, and before returning to the caller.  So sometimes,
  3090.      * the fastmap loop is restarted with `continue', othertimes by
  3091.      * `goto init_fastmap_sentinal'.
  3092.      */
  3093.     if (size)
  3094.       {
  3095.     fastmap_chr = ((search_direction == 1)
  3096.                ? *(end - 1)
  3097.                : *string);
  3098.     fastmap_val = fastmap[fastmap_chr];
  3099.     fastmap[fastmap_chr] = 1;
  3100.       }
  3101.     else
  3102.       fastmap_chr = -1;
  3103.  
  3104.     do
  3105.       {
  3106.     /* If we haven't reached the end of a string half, and if the
  3107.      * pattern can't match the empty string, then the fastmap 
  3108.      * optimization applies.  This conditional scans using the 
  3109.      * fastmap -- stoping when a string half ends, or when a 
  3110.      * plausible starting point for a match is found.
  3111.      * It updates HIT_BOUND to tell which case occured.
  3112.      */
  3113.     if (pos == end)
  3114.       goto fastmap_hit_bound;
  3115.     else
  3116.       {
  3117.         if (search_direction == 1)
  3118.           {
  3119.         if (fastmap_val)
  3120.           {
  3121.             for (;;)
  3122.               {
  3123.             while (!fastmap[*pos])
  3124.               ++pos;
  3125.             goto commence_a_matchin;
  3126.               }
  3127.           }
  3128.         else
  3129.           {
  3130.             for (;;)
  3131.               {
  3132.             while (!fastmap[*pos])
  3133.               ++pos;
  3134.             if (*pos != fastmap_chr)
  3135.               goto commence_a_matchin;
  3136.             else 
  3137.               {
  3138.                 ++pos;
  3139.                 if (pos == end)
  3140.                   goto fastmap_hit_bound;
  3141.               }
  3142.               }
  3143.           }
  3144.           }
  3145.         else
  3146.           {
  3147.         const unsigned char * bound = string - 1;
  3148.         for (;;)
  3149.           {
  3150.             while (!fastmap[*pos])
  3151.               --pos;
  3152.             if ((*pos != fastmap_chr) || fastmap_val)
  3153.               goto commence_a_matchin;
  3154.             else 
  3155.               {
  3156.             --pos;
  3157.             if (pos == bound)
  3158.               goto fastmap_hit_bound;
  3159.               }
  3160.           }
  3161.           }
  3162.       }
  3163.     
  3164.       fastmap_hit_bound:
  3165.     {
  3166.       /* If we hit a bound, it may simply be time to switch sides
  3167.        * between strings.
  3168.        */
  3169.       if ((search_direction == 1) && string2 && (half == 0))
  3170.         {
  3171.           string = (const unsigned char *)string2;
  3172.           size = size2;
  3173.           offset = size1;
  3174.           half = 1;
  3175.           end = (const unsigned char *)MIN(string2 + size2,
  3176.                            string2 + stop - size1);
  3177.           startpos = size1;
  3178.           pos = (const unsigned char *)string2;
  3179.           goto init_fastmap_sentinal;
  3180.         }
  3181.       else if (   string1
  3182.            && (search_direction == -1)
  3183.            && (half == 1))
  3184.         {
  3185.           string = (const unsigned char *)string1;
  3186.           size = size1;
  3187.           offset = 0;
  3188.           end = (const unsigned char *)string1 + size1;
  3189.           half = 0;
  3190.           startpos = size1 - 1;
  3191.           pos = (const unsigned char *)string1 + size1 - 1;
  3192.           goto init_fastmap_sentinal;
  3193.         }
  3194.       /* ...not a string split, simply no more string. 
  3195.        *
  3196.        * When searching backward, running out of string
  3197.        * is reason to quit.
  3198.        */
  3199.       else if (search_direction == -1)
  3200.         goto finish;
  3201.       
  3202.       /* ...when searching forward, we allow the possibility
  3203.        * of an (empty) match after the last character in the
  3204.        * virtual string.  So, fall through to the matcher
  3205.        */
  3206.     }
  3207.  
  3208.  
  3209.       commence_a_matchin:
  3210.  
  3211.     /***** fastmap/search loop body
  3212.      *          test for a match that begins at pos
  3213.      */
  3214.  
  3215.     /* Now the fastmap loop has brought us to a plausible 
  3216.      * starting point for a match.  So, it's time to run the
  3217.      * NFA and see if a match occured.
  3218.      */
  3219.  
  3220.     startpos = pos - string + offset;
  3221.     if (startpos == search_end)
  3222.       goto finish;
  3223.     
  3224.     last_l = last_r = 0;
  3225.     lparen[0] = startpos;    /* We know match-begin for this test... */
  3226.  
  3227.     /* The test matcher is essentially a recursive function
  3228.      * that does an exhaustive run of the superNFA at the 
  3229.      * test position.  For performance, that function has 
  3230.      * been in-lined by hand.
  3231.      */
  3232.  
  3233. #undef OF
  3234. #ifndef HAVE_GNUC_LABELS
  3235. #define OF(A,B)    A
  3236. #else
  3237. #define OF(A,B)    A: B
  3238.       static void * rx_labels_instruction_table[] =
  3239.         {
  3240.           [rx_backtrack_point] &&backtrack_point,
  3241.           [rx_backtrack] &&backtrack,
  3242.           [rx_do_side_effects] &&do_side_effects,
  3243.           [rx_cache_miss] &&cache_miss,
  3244.           [rx_next_char] 0,
  3245.           [rx_error_inx] 0
  3246.         };
  3247. #endif
  3248.     {      
  3249.       /* The current superNFA position of the matcher. */
  3250.       struct rx_superstate * super = start_super;
  3251.       
  3252.       /* The matcher interprets a series of instruction frames.
  3253.        * This is the `instruction counter' for the interpretation.
  3254.        */
  3255.       struct rx_inx * ifr;
  3256.       
  3257.       /* We insert a ghost character in the string to prime
  3258.        * the nfa.  tst_pos, tst_str_half, and tst_end_half
  3259.        * keep track of the test-match position and string-half.
  3260.        */
  3261.       const unsigned char * tst_pos = pos - 1;
  3262.       int tst_half = half;
  3263.       unsigned char c = nfa_choice;
  3264.       
  3265.       const unsigned char * tst_str_half = string;
  3266.       const unsigned char * tst_end_half = end;
  3267.       
  3268.       struct stack_chunk * counter_stack = 0;
  3269.       struct stack_chunk * backtrack_stack = 0;
  3270.       int backtrack_frame_bytes =
  3271.         (sizeof (struct backtrack_frame)
  3272.          + (rxb->match_regs_on_stack
  3273.         ? sizeof (regoff_t) * (num_regs + 1) * 2
  3274.         : 0));
  3275.       int chunk_bytes = backtrack_frame_bytes * 64;
  3276.       struct stack_chunk * free_chunks = 0;
  3277.  
  3278. #ifdef RX_DEBUG
  3279.       int backtrack_depth = 0;
  3280. #endif
  3281.  
  3282.       /* To return from this function, set test_ret and 
  3283.        * `goto test_do_return'.
  3284.        *
  3285.        * Possible return values are:
  3286.        *     1   --- end of string while the superNFA is still going
  3287.        *     0   --- internal error (out of memory)
  3288.        *    -1   --- search completed by reaching the superNFA fail state
  3289.        *    -2   --- a match was found, maybe not the longest.
  3290.        *
  3291.        * When the search is complete (-1), best_last_r indicates whether
  3292.        * a match was found.
  3293.        *
  3294.        * -2 is return only if first_found is non-zero.
  3295.        *
  3296.        * if first_found is non-zero, a return of -1 indicates no match,
  3297.        * otherwise, best_last_r has to be checked.
  3298.        */
  3299.       int test_ret = -1;
  3300.       
  3301.       while (1)
  3302.         {
  3303.           int inx;
  3304. #ifdef RX_DEBUG
  3305.           /* There is a search tree with every node as set of deterministic
  3306.            * transitions in the super nfa.  For every branch of a 
  3307.            * backtrack point is an edge in the tree.
  3308.            * This counts up a pre-order of nodes in that tree.
  3309.            * It's saved on the search stack and printed when debugging. 
  3310.            */
  3311.           int line_no = 0;
  3312.           int lines_found = 0;
  3313. #endif
  3314.  
  3315.  
  3316.         top_of_cycle:
  3317.           /* A superstate is basicly a transition table, indexed by 
  3318.            * characters from the string being tested, and containing 
  3319.            * RX_INX structures.
  3320.            */
  3321.           ifr = &super->transitions [c];
  3322.           
  3323.         recurse_test_match:
  3324.           /* This is the point to which control is sent when the
  3325.            * test matcher recurses.  Before jumping here, some variables
  3326.            * need to be saved on the stack and setup for the recursion.
  3327.            */
  3328.  
  3329.         restart:
  3330.           /* Some instructions don't advance the matcher, but just
  3331.            * carry out some side effects and fetch a new instruction.
  3332.            * To dispatch that new instruction, `goto restart'.
  3333.            */
  3334.           
  3335.           {
  3336.         struct rx_inx * next_tr_table = (struct rx_inx *)ifr->data;
  3337.         struct rx_inx * this_tr_table = super->transitions;
  3338.         /* The fastest route through the loop is when the instruction 
  3339.          * is RX_NEXT_CHAR.  This case is detected when IFR->DATA
  3340.          * is non-zero.  In that case, it points to the next
  3341.          * superstate. 
  3342.          *
  3343.          * This allows us to not bother fetching the bytecode.
  3344.          */
  3345.         while (next_tr_table)
  3346.           {
  3347. #ifdef RX_DEBUG
  3348.             if (rx_debug_trace)
  3349.               {
  3350.             struct rx_superset * setp;
  3351.  
  3352.             fprintf (stderr, "%d %d>> re_next_char @ %d (%d)",
  3353.                  line_no,
  3354.                  backtrack_depth,
  3355.                  (tst_pos - tst_str_half
  3356.                   + (tst_half == 0
  3357.                      ? 0 : size1)), c);
  3358.             
  3359.             super =
  3360.               ((struct rx_superstate *)
  3361.                ((char *)this_tr_table
  3362.                 - ((unsigned long)
  3363.                    ((struct rx_superstate *)0)->transitions)));
  3364.  
  3365.             setp = super->contents;
  3366.             fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  3367.                  rxb->rx.rx_id, setp);
  3368.             while (setp)
  3369.               {
  3370.                 fprintf (stderr, "%d ", setp->id);
  3371.                 setp = setp->cdr;
  3372.               }
  3373.             fprintf (stderr, "\n");
  3374.               }
  3375. #endif
  3376.             this_tr_table = next_tr_table;
  3377.             ++tst_pos;
  3378.             if (tst_pos == tst_end_half)
  3379.               {
  3380.             if (   (tst_pos != abs_end)
  3381.                 && string2
  3382.                 && half == 0)
  3383.               {
  3384.                 /* Here we are crossing the break 
  3385.                  * in a split string. 
  3386.                  */
  3387.                 tst_str_half = (const unsigned char *)string2;
  3388.                 tst_end_half = abs_end;
  3389.                 tst_pos = (const unsigned char *)string2;
  3390.                 tst_half = 1;
  3391.               }
  3392.             else
  3393.               {
  3394.                 test_ret = 1;
  3395.                 goto test_do_return;
  3396.               }
  3397.               }
  3398.             c = *tst_pos;
  3399.             ifr = this_tr_table + c;
  3400.             next_tr_table = (struct rx_inx *)ifr->data;
  3401.           }
  3402.         
  3403.         /* Here when we ran out cached next-char transitions. 
  3404.          * So, it will be necessary to do a more expensive
  3405.          * dispatch on the current instruction.  The superstate
  3406.          * pointer is allowed to become invalid during next-char
  3407.          * transitions -- now we must bring it up to date.
  3408.          */
  3409.         super =
  3410.           ((struct rx_superstate *)
  3411.            ((char *)this_tr_table
  3412.             - ((unsigned long)
  3413.                ((struct rx_superstate *)0)->transitions)));
  3414.           }
  3415.           
  3416.           /* We've encountered an instruction other than next-char.
  3417.            * Dispatch that instruction:
  3418.            */
  3419.           inx = (int)ifr->inx;
  3420. #ifdef HAVE_GNUC_LABELS
  3421.           goto *rx_labels_instruction_table[inx];
  3422. #endif
  3423. #ifdef RX_DEBUG
  3424.           if (rx_debug_trace)
  3425.         {
  3426.           struct rx_superset * setp = super->contents;
  3427.           
  3428.           fprintf (stderr, "%d %d>> %s @ %d (%d)", line_no,
  3429.                backtrack_depth,
  3430.                inx_names[inx],
  3431.                (tst_pos - tst_str_half
  3432.                 + (tst_half == 0 ? 0 : size1)), c);
  3433.           
  3434.           fprintf (stderr, "   superstet (rx=%d, &=%x: ",
  3435.                rxb->rx.rx_id, setp);
  3436.           while (setp)
  3437.             {
  3438.               fprintf (stderr, "%d ", setp->id);
  3439.               setp = setp->cdr;
  3440.             }
  3441.           fprintf (stderr, "\n");
  3442.         }
  3443. #endif
  3444.           switch ((enum rx_opcode)inx)
  3445.         {
  3446.         case OF(rx_do_side_effects,do_side_effects):
  3447.  
  3448.           /*  RX_DO_SIDE_EFFECTS occurs when we cross epsilon 
  3449.            *  edges associated with parentheses, backreferencing, etc.
  3450.            */
  3451.           {
  3452.             struct rx_distinct_future * df =
  3453.               (struct rx_distinct_future *)ifr->data_2;
  3454.             struct rx_se_list * el = df->effects;
  3455.             /* Side effects come in lists.  This walks down
  3456.              * a list, dispatching.
  3457.              */
  3458.             while (el)
  3459.               {
  3460. #ifdef HAVE_GNUC_LABELS
  3461.             static void * se_labels[] =
  3462.               {
  3463.                 [-re_se_try] &&se_try,
  3464.                 [-re_se_pushback] &&se_pushback,
  3465.                 [-re_se_push0] &&se_push0,
  3466.                 [-re_se_pushpos] &&se_pushpos,
  3467.                 [-re_se_chkpos] &&se_chkpos,
  3468.                 [-re_se_poppos] &&se_poppos,
  3469. #ifdef emacs
  3470.                 [-re_se_at_dot] &&se_at_dot,
  3471.                 [-re_se_syntax] &&se_syntax,
  3472.                 [-re_se_not_syntax] &&se_not_syntax,
  3473. #endif
  3474.                 [-re_se_begbuf] &&se_begbuf,
  3475.                 [-re_se_hat] &&se_hat,
  3476.                 [-re_se_wordbeg] &&se_wordbeg,
  3477.                 [-re_se_wordbound] &&se_wordbound,
  3478.                 [-re_se_notwordbound] &&se_notwordbound,
  3479.                 [-re_se_wordend] &&se_wordend,
  3480.                 [-re_se_endbuf] &&se_endbuf,
  3481.                 [-re_se_dollar] &&se_dollar,
  3482.                 [-re_se_fail] &&se_fail,
  3483.               };
  3484.             static void * se_lables2[] =
  3485.               {
  3486.                 [re_se_win] &&se_win
  3487.                 [re_se_lparen] &&se_lparen,
  3488.                 [re_se_rparen] &&se_rparen,
  3489.                 [re_se_backref] &&se_backref,
  3490.                 [re_se_iter] &&se_iter,
  3491.                 [re_se_end_iter] &&se_end_iter,
  3492.                 [re_se_tv] &&se_tv
  3493.               };
  3494. #endif
  3495.             int effect = (int)el->car;
  3496.             if (effect < 0)
  3497.               {
  3498. #ifdef HAVE_GNUC_LABELS
  3499.                 goto *se_labels[-effect];
  3500. #endif
  3501. #ifdef RX_DEBUG
  3502.                 if (rx_debug_trace)
  3503.                   {
  3504.                 struct rx_superset * setp = super->contents;
  3505.                 
  3506.                 fprintf (stderr, "....%d %d>> %s\n", line_no,
  3507.                      backtrack_depth,
  3508.                      efnames[-effect]);
  3509.                   }
  3510. #endif
  3511.                 switch ((enum re_side_effects) effect)
  3512.                   {
  3513.                   case OF(re_se_pushback,se_pushback):
  3514.                 ifr = &df->future_frame;
  3515.                 if (!ifr->data)
  3516.                   {
  3517.                     struct rx_superstate * sup = super;
  3518.                     rx_lock_superstate (rx, sup);
  3519.                     if (!rx_handle_cache_miss (&rxb->rx,
  3520.                                    super, c,
  3521.                                    ifr->data_2))
  3522.                       {
  3523.                     rx_unlock_superstate (rx, sup);
  3524.                     test_ret = 0;
  3525.                     goto test_do_return;
  3526.                       }
  3527.                     rx_unlock_superstate (rx, sup);
  3528.                   }
  3529.                 /* --tst_pos; */
  3530.                 c = 't';
  3531.                 super
  3532.                   = ((struct rx_superstate *)
  3533.                      ((char *)ifr->data
  3534.                       - (long)(((struct rx_superstate *)0)
  3535.                            ->transitions)));
  3536.                 goto top_of_cycle;
  3537.                 break;
  3538.                   case OF(re_se_push0,se_push0):
  3539.                 {
  3540.                   struct counter_frame * old_cf
  3541.                      = (counter_stack
  3542.                     ? ((struct counter_frame *)
  3543.                        counter_stack->sp)
  3544.                     : 0);
  3545.                   struct counter_frame * cf;
  3546.                   PUSH (counter_stack,
  3547.                     sizeof (struct counter_frame));
  3548.                   cf = ((struct counter_frame *)
  3549.                     counter_stack->sp);
  3550.                   cf->tag = re_se_iter;
  3551.                   cf->val = 0;
  3552.                   cf->inherited_from = 0;
  3553.                   cf->cdr = old_cf;
  3554.                   break;
  3555.                 }
  3556.                   case OF(re_se_fail,se_fail):
  3557.                 goto test_do_return;
  3558.                   case OF(re_se_begbuf,se_begbuf):
  3559.                 if (!AT_STRINGS_BEG ())
  3560.                   goto test_do_return;
  3561.                 break;
  3562.                   case OF(re_se_endbuf,se_endbuf):
  3563.                 if (!AT_STRINGS_END ())
  3564.                   goto test_do_return;
  3565.                 break;
  3566.                   case OF(re_se_wordbeg,se_wordbeg):
  3567.                 if (   LETTER_P (tst_pos + 1)
  3568.                     && (   AT_STRINGS_BEG()
  3569.                     || !LETTER_P (tst_pos)))
  3570.                   break;
  3571.                 else
  3572.                   goto test_do_return;
  3573.                   case OF(re_se_wordend,se_wordend):
  3574.                 if (   !AT_STRINGS_BEG ()
  3575.                     && LETTER_P (tst_pos)
  3576.                     && (AT_STRINGS_END ()
  3577.                     || !LETTER_P (tst_pos + 1)))
  3578.                   break;
  3579.                 else
  3580.                   goto test_do_return;
  3581.                   case OF(re_se_wordbound,se_wordbound):
  3582.                 if (AT_WORD_BOUNDARY (tst_pos))
  3583.                   break;
  3584.                 else
  3585.                   goto test_do_return;
  3586.                   case OF(re_se_notwordbound,se_notwordbound):
  3587.                 if (!AT_WORD_BOUNDARY (tst_pos))
  3588.                   break;
  3589.                 else
  3590.                   goto test_do_return;
  3591.                   case OF(re_se_hat,se_hat):
  3592.                 if (AT_STRINGS_BEG ())
  3593.                   {
  3594.                     if (rxb->not_bol)
  3595.                       goto test_do_return;
  3596.                     else
  3597.                       break;
  3598.                   }
  3599.                 else
  3600.                   {
  3601.                     char pos_c = *tst_pos;
  3602.                     if (   (TRANSLATE (pos_c)
  3603.                         == TRANSLATE('\n'))
  3604.                     && rxb->newline_anchor)
  3605.                       break;
  3606.                     else
  3607.                       goto test_do_return;
  3608.                   }
  3609.                   case OF(re_se_dollar,se_dollar):
  3610.                 if (AT_STRINGS_END ())
  3611.                   {
  3612.                     if (rxb->not_eol)
  3613.                       goto test_do_return;
  3614.                     else
  3615.                       break;
  3616.                   }
  3617.                 else
  3618.                   {
  3619.                     const unsigned char * next_pos
  3620.                       = ((string2 && (tst_half == 0) &&
  3621.                       (tst_pos
  3622.                        == ((unsigned char *)
  3623.                            string1 + size1 - 1)))
  3624.                      ? (unsigned char *)string2
  3625.                      : tst_pos + 1);
  3626.                     
  3627.                     if (   (TRANSLATE (*next_pos)
  3628.                         == TRANSLATE ('\n'))
  3629.                     && rxb->newline_anchor)
  3630.                       break;
  3631.                     else
  3632.                       goto test_do_return;
  3633.                   }
  3634.                 
  3635.                   case OF(re_se_try,se_try):
  3636.                 /* This is the first side effect in every
  3637.                  * expression.
  3638.                  *
  3639.                  *  FOR NO GOOD REASON...get rid of it...
  3640.                  */
  3641.                 break;
  3642.  
  3643.                   case OF(re_se_pushpos,se_pushpos):
  3644.                 {
  3645.                   int urhere =
  3646.                     ((int)(tst_pos - tst_str_half)
  3647.                      + ((tst_half == 0) ? 0 : size1));
  3648.                   struct counter_frame * old_cf
  3649.                     = (counter_stack
  3650.                        ? ((struct counter_frame *)
  3651.                       counter_stack->sp)
  3652.                        : 0);
  3653.                   struct counter_frame * cf;
  3654.                   PUSH(counter_stack,
  3655.                        sizeof (struct counter_frame));
  3656.                   cf = ((struct counter_frame *)
  3657.                     counter_stack->sp);
  3658.                   cf->tag = re_se_pushpos;
  3659.                   cf->val = urhere;
  3660.                   cf->inherited_from = 0;
  3661.                   cf->cdr = old_cf;
  3662.                   break;
  3663.                 }
  3664.                 
  3665.                   case OF(re_se_chkpos,se_chkpos):
  3666.                 {
  3667.                   int urhere =
  3668.                     ((int)(tst_pos - tst_str_half)
  3669.                      + ((tst_half == 0) ? 0 : size1));
  3670.                   struct counter_frame * cf
  3671.                     = ((struct counter_frame *)
  3672.                        counter_stack->sp);
  3673.                   if (cf->val == urhere)
  3674.                     goto test_do_return;
  3675.                   cf->val = urhere;
  3676.                   break;
  3677.                 }
  3678.                 break;
  3679.  
  3680.                   case OF(re_se_poppos,se_poppos):
  3681.                 POP(counter_stack,
  3682.                     sizeof (struct counter_frame));
  3683.                 break;
  3684.                 
  3685.                 
  3686.                   case OF(re_se_at_dot,se_at_dot):
  3687.                   case OF(re_se_syntax,se_syntax):
  3688.                   case OF(re_se_not_syntax,se_not_syntax):
  3689. #ifdef emacs
  3690.                 this release lacks emacs support;
  3691.                 (coming soon);
  3692. #endif
  3693.                 break;
  3694.                   case re_se_win:
  3695.                   case re_se_lparen:
  3696.                   case re_se_rparen:
  3697.                   case re_se_backref:
  3698.                   case re_se_iter:
  3699.                   case re_se_end_iter:
  3700.                   case re_se_tv:
  3701.                   case re_floogle_flap:
  3702.                 ret_val = 0;
  3703.                 goto test_do_return;
  3704.                   }
  3705.               }
  3706.             else
  3707.               {
  3708. #ifdef HAVE_GNUC_LABELS
  3709.                 goto *se_lables2[(rxb->se_params [effect].se)];
  3710. #endif
  3711. #ifdef RX_DEBUG
  3712.               if (rx_debug_trace)
  3713.                 fprintf (stderr, "....%d %d>> %s %d %d\n", line_no,
  3714.                      backtrack_depth,
  3715.                      efnames2[rxb->se_params [effect].se],
  3716.                      rxb->se_params [effect].op1,
  3717.                      rxb->se_params [effect].op2);
  3718. #endif
  3719.                 switch (rxb->se_params [effect].se)
  3720.                   {
  3721.                   case OF(re_se_win,se_win):
  3722.                 /* This side effect indicates that we've 
  3723.                  * found a match, though not necessarily the 
  3724.                  * best match.  This is a fancy assignment to 
  3725.                  * register 0 unless the caller didn't 
  3726.                  * care about registers.  In which case,
  3727.                  * this stops the match.
  3728.                  */
  3729.                 {
  3730.                   int urhere =
  3731.                     ((int)(tst_pos - tst_str_half)
  3732.                      + ((tst_half == 0)
  3733.                     ? 0 : size1));
  3734.  
  3735.                   if (   (best_last_r < 0)
  3736.                       || (urhere + 1 > best_rparen[0]))
  3737.                     {
  3738.                       /* Record the best known and keep
  3739.                        * looking.
  3740.                        */
  3741.                       int x;
  3742.                       for (x = 0; x <= last_l; ++x)
  3743.                     best_lparen[x] = lparen[x];
  3744.                       best_last_l = last_l;
  3745.                       for (x = 0; x <= last_r; ++x)
  3746.                     best_rparen[x] = rparen[x];
  3747.                       best_rparen[0] = urhere + 1;
  3748.                       best_last_r = last_r;
  3749.                     }
  3750.                   /* If we're not reporting the match-length 
  3751.                    * or other register info, we need look no
  3752.                    * further.
  3753.                    */
  3754.                   if (first_found)
  3755.                     {
  3756.                       test_ret = -2;
  3757.                       goto test_do_return;
  3758.                     }
  3759.                 }
  3760.                 break;
  3761.                   case OF(re_se_lparen,se_lparen):
  3762.                 {
  3763.                   int urhere =
  3764.                     ((int)(tst_pos - tst_str_half)
  3765.                      + ((tst_half == 0) ? 0 : size1));
  3766.                   
  3767.                   int reg = rxb->se_params [effect].op1;
  3768. #if 0
  3769.                   if (reg > last_l)
  3770. #endif
  3771.                     {
  3772.                       lparen[reg] = urhere + 1;
  3773.                       /* In addition to making this assignment,
  3774.                        * we now know that lower numbered regs
  3775.                        * that haven't already been assigned,
  3776.                        * won't be.  We make sure they're
  3777.                        * filled with -1, so they can be
  3778.                        * recognized as unassigned.
  3779.                        */
  3780.                       if (last_l < reg)
  3781.                     while (++last_l < reg)
  3782.                       lparen[last_l] = -1;
  3783.                     }
  3784.                   break;
  3785.                 }
  3786.                 
  3787.                   case OF(re_se_rparen,se_rparen):
  3788.                 {
  3789.                   int urhere =
  3790.                     ((int)(tst_pos - tst_str_half)
  3791.                      + ((tst_half == 0) ? 0 : size1));
  3792.                   int reg = rxb->se_params [effect].op1;
  3793.                   rparen[reg] = urhere + 1;
  3794.                   if (last_r < reg)
  3795.                     {
  3796.                       while (++last_r < reg)
  3797.                     rparen[last_r] = -1;
  3798.                     }
  3799.                   break;
  3800.                 }
  3801.                 
  3802.                   case OF(re_se_backref,se_backref):
  3803.                 {
  3804.                   int reg = rxb->se_params [effect].op1;
  3805.                   if (reg > last_r || rparen[reg] < 0)
  3806.                     goto test_do_return;
  3807.                   {
  3808.                     /* fixme */
  3809.                     const unsigned char * there
  3810.                       = tst_str_half + lparen[reg];
  3811.                     const unsigned char * last
  3812.                       = tst_str_half + rparen[reg];
  3813.                     const unsigned char * here = tst_pos + 1;
  3814.  
  3815.                     if ((here == tst_end_half) && string2
  3816.                     && (tst_str_half
  3817.                         == (unsigned char *) string1)
  3818.                     && (tst_end_half != abs_end))
  3819.                       {
  3820.                     here = (unsigned char *)string2;
  3821.                     tst_end_half = abs_end;
  3822.                       }
  3823.                     
  3824.                     while (there < last && here < tst_end_half)    /* 4% */
  3825.                       if (TRANSLATE(*there) /* &&&& 6% */
  3826.                       != TRANSLATE(*here))
  3827.                     goto test_do_return;
  3828.                       else
  3829.                     {
  3830.                       ++there; ++here;
  3831.                       if ((here == tst_end_half) && string2
  3832.                           && (tst_str_half
  3833.                           == (unsigned char *)string1)
  3834.                           && (tst_end_half != abs_end))
  3835.                         {
  3836.                           here = (unsigned char *)string2;
  3837.                           tst_end_half = abs_end;
  3838.                           tst_half = 1;
  3839.                         }
  3840.                     }
  3841.                     if (there != last)
  3842.                       goto test_do_return;
  3843.                     tst_pos = here - 1;
  3844.                     if ((here == (unsigned char *)string2)
  3845.                     && (unsigned char *)string1)
  3846.                       {
  3847.                     tst_pos = ((unsigned char *)string1
  3848.                            + size1 - 1);
  3849.                     tst_end_half = tst_pos + 1;
  3850.                     tst_half = 0;
  3851.                       }
  3852.                   }
  3853.                   break;
  3854.                 }
  3855.                   case OF(re_se_iter,se_iter):
  3856.                 {
  3857.                   struct counter_frame * csp
  3858.                     = ((struct counter_frame *)
  3859.                        counter_stack->sp);
  3860.                   if (csp->val == rxb->se_params[effect].op2)
  3861.                     goto test_do_return;
  3862.                   else
  3863.                     ++csp->val;
  3864.                   break;
  3865.                 }
  3866.                   case OF(re_se_end_iter,se_end_iter):
  3867.                 {
  3868.                   struct counter_frame * csp
  3869.                     = ((struct counter_frame *)
  3870.                        counter_stack->sp);
  3871.                   if (csp->val < rxb->se_params[effect].op1)
  3872.                     goto test_do_return;
  3873.                   else
  3874.                     {
  3875.                       struct counter_frame * source = csp;
  3876.                       while (source->inherited_from)
  3877.                     source = source->inherited_from;
  3878.                       if (!source || !source->cdr)
  3879.                     {
  3880.                       POP(counter_stack,
  3881.                           sizeof(struct counter_frame));
  3882.                     }
  3883.                       else
  3884.                     {
  3885.                       source = source->cdr;
  3886.                       csp->val = source->val;
  3887.                       csp->tag = source->tag;
  3888.                       csp->cdr = 0;
  3889.                       csp->inherited_from = source;
  3890.                     }
  3891.                     }
  3892.                   break;
  3893.                 }
  3894.                   case OF(re_se_tv, se_tv):
  3895.                 /* is a noop */
  3896.                 break;
  3897.                   case re_se_try:
  3898.                   case re_se_pushback:
  3899.                   case re_se_push0:
  3900.                   case re_se_pushpos:
  3901.                   case re_se_chkpos:
  3902.                   case re_se_poppos:
  3903.                   case re_se_at_dot:
  3904.                   case re_se_syntax:
  3905.                   case re_se_not_syntax:
  3906.                   case re_se_begbuf:
  3907.                   case re_se_hat:
  3908.                   case re_se_wordbeg:
  3909.                   case re_se_wordbound:
  3910.                   case re_se_notwordbound:
  3911.                   case re_se_wordend:
  3912.                   case re_se_endbuf:
  3913.                   case re_se_dollar:
  3914.                   case re_se_fail:
  3915.                   case re_floogle_flap:
  3916.                 ret_val = 0;
  3917.                 goto test_do_return;
  3918.                   }
  3919.               }
  3920.             el = el->cdr;
  3921.               }
  3922.             /* Now the side effects are done,
  3923.              * so get the next instruction.
  3924.              * and move on.
  3925.              */
  3926.             ifr = &df->future_frame;
  3927.             goto restart;
  3928.           }
  3929.           
  3930.         case OF(rx_backtrack_point,backtrack_point):
  3931.           {
  3932.             /* A backtrack point indicates that we've reached a
  3933.              * non-determinism in the superstate NFA.  This is a
  3934.              * loop that exhaustively searches the possibilities.
  3935.              *
  3936.              * A backtracking strategy is used.  We keep track of what
  3937.              * registers are valid so we can erase side effects.
  3938.              *
  3939.              * First, make sure there is some stack space to hold 
  3940.              * our state.
  3941.              */
  3942.  
  3943.             struct backtrack_frame * bf;
  3944.  
  3945.             PUSH(backtrack_stack, backtrack_frame_bytes);
  3946. #ifdef RX_DEBUG
  3947.             ++backtrack_depth;
  3948. #endif
  3949.  
  3950.             bf = ((struct backtrack_frame *)
  3951.               backtrack_stack->sp);
  3952.             {
  3953.               bf->stk_super = super;
  3954.               /* We prevent the current superstate from being
  3955.                * deleted from the superstate cache.
  3956.                */
  3957.               rx_lock_superstate (&rxb->rx, super);
  3958.               bf->stk_tst_pos = tst_pos;
  3959. #ifdef RX_DEBUG
  3960.               bf->stk_line_no = line_no;
  3961. #endif
  3962.               bf->stk_tst_half = tst_half;
  3963.               bf->stk_c = c;
  3964.               bf->stk_tst_str_half = tst_str_half;
  3965.               bf->stk_tst_end_half = tst_end_half;
  3966.               bf->stk_last_l = last_l;
  3967.               bf->stk_last_r = last_r;
  3968.               bf->df = ((struct rx_super_edge *)ifr->data_2)->options;
  3969.               bf->first_df = bf->df;
  3970.               bf->counter_stack_sp = (counter_stack
  3971.                           ? counter_stack->sp
  3972.                           : 0);
  3973.               bf->stk_test_ret = test_ret;
  3974.               if (rxb->match_regs_on_stack)
  3975.             {
  3976.               int x;
  3977.               regoff_t * stk =
  3978.                 (regoff_t *)((char *)bf + sizeof (*bf));
  3979.               for (x = 0; x <= last_l; ++x)
  3980.                 stk[x] = lparen[x];
  3981.               stk += x;
  3982.               for (x = 0; x <= last_r; ++x)
  3983.                 stk[x] = rparen[x];
  3984.             }
  3985.  
  3986.             }
  3987.  
  3988.             /* Here is a while loop whose body is mainly a function
  3989.              * call and some code to handle a return from that
  3990.              * function.
  3991.              *
  3992.              * From here on for the rest of `case backtrack_point' it
  3993.              * is unsafe to assume that the variables saved on the
  3994.              * stack are valid -- so reread their values from the stack
  3995.              * as needed.
  3996.              *
  3997.              * This lets us re-use one generation fewer stack saves in
  3998.              * the call-graph of a search.
  3999.              */
  4000.             
  4001.           while_non_det_options:
  4002. #ifdef RX_DEBUG
  4003.             ++lines_found;
  4004.             if (rx_debug_trace)
  4005.               fprintf (stderr, "@@@ %d calls %d @@@\n",
  4006.                    line_no, lines_found);
  4007.             
  4008.             line_no = lines_found;
  4009. #endif
  4010.             
  4011.             if (bf->df->next_same_super_edge[0] == bf->first_df)
  4012.               {
  4013.             /* This is a tail-call optimization -- we don't recurse
  4014.              * for the last of the possible futures.
  4015.              */
  4016.             ifr = (bf->df->effects
  4017.                    ? &bf->df->side_effects_frame
  4018.                    : &bf->df->future_frame);
  4019.  
  4020.             rx_unlock_superstate (&rxb->rx, super);
  4021.             POP(backtrack_stack, backtrack_frame_bytes);
  4022. #ifdef RX_DEBUG
  4023.             --backtrack_depth;
  4024. #endif
  4025.             goto restart;
  4026.               }
  4027.             else
  4028.               {
  4029.             if (counter_stack)
  4030.               {
  4031.                 struct counter_frame * old_cf
  4032.                   = ((struct counter_frame *)counter_stack->sp);
  4033.                 struct counter_frame * cf;
  4034.                 PUSH(counter_stack, sizeof (struct counter_frame));
  4035.                 cf = ((struct counter_frame *)counter_stack->sp);
  4036.                 cf->tag = old_cf->tag;
  4037.                 cf->val = old_cf->val;
  4038.                 cf->inherited_from = old_cf;
  4039.                 cf->cdr = 0;
  4040.               }            
  4041.             /* `Call' this test-match block */
  4042.             ifr = (bf->df->effects
  4043.                    ? &bf->df->side_effects_frame
  4044.                    : &bf->df->future_frame);
  4045.             goto recurse_test_match;
  4046.               }
  4047.  
  4048.             /* Returns in this block are accomplished by
  4049.              * goto test_do_return.  There are two cases.
  4050.              * If there is some search-stack left,
  4051.              * then it is a return from a `recursive' call.
  4052.              * If there is no search-stack left, then
  4053.              * we should return to the fastmap/search loop.
  4054.              */
  4055.             
  4056.           test_do_return:
  4057.  
  4058.             if (!backtrack_stack)
  4059.               {
  4060. #ifdef RX_DEBUG
  4061.             if (rx_debug_trace)
  4062.               fprintf (stderr, "!!! %d bails returning %d !!!\n",
  4063.                    line_no, test_ret);
  4064. #endif
  4065.  
  4066.             /* No more search-stack -- this test is done. */
  4067.             if (test_ret)
  4068.               goto return_from_test_match;
  4069.             else
  4070.               goto error_in_testing_match;
  4071.               }
  4072.  
  4073.             /* Ok..we're returning from a recursive call to 
  4074.              * the test match block:
  4075.              */
  4076.             
  4077.             bf = ((struct backtrack_frame *)
  4078.               backtrack_stack->sp);
  4079. #ifdef RX_DEBUG
  4080.             if (rx_debug_trace)
  4081.               fprintf (stderr, "+++ %d returns %d (to %d)+++\n",
  4082.                    line_no, test_ret, bf->stk_line_no);
  4083. #endif
  4084.  
  4085.             while (counter_stack
  4086.                && (!bf->counter_stack_sp
  4087.                    || (bf->counter_stack_sp != counter_stack->sp)))
  4088.               {
  4089.             POP(counter_stack, sizeof (struct counter_frame));
  4090.               }
  4091.  
  4092.             if (!test_ret)
  4093.               {
  4094.             POP (backtrack_stack, backtrack_frame_bytes);
  4095.             goto test_do_return;
  4096.               }
  4097.  
  4098.             /* If any possible future reaches the end of the 
  4099.              * string without failing, make sure we propogate 
  4100.              * that information to the caller.
  4101.              */
  4102.             if ((test_ret == -2) && first_found)
  4103.               {
  4104.             rx_unlock_superstate (&rxb->rx, bf->stk_super);
  4105.             POP (backtrack_stack, backtrack_frame_bytes);
  4106.             goto test_do_return;
  4107.               }
  4108.  
  4109.             if (bf->stk_test_ret < 0)
  4110.               test_ret = bf->stk_test_ret;
  4111.  
  4112.             last_l = bf->stk_last_l;
  4113.             last_r = bf->stk_last_r;
  4114.             bf->df = bf->df->next_same_super_edge[0];
  4115.             super = bf->stk_super;
  4116.             tst_pos = bf->stk_tst_pos;
  4117.             tst_half = bf->stk_tst_half;
  4118.             c = bf->stk_c;
  4119.             tst_str_half = bf->stk_tst_str_half;
  4120.             tst_end_half = bf->stk_tst_end_half;
  4121. #ifdef RX_DEBUG
  4122.             line_no = bf->stk_line_no;
  4123. #endif
  4124.  
  4125.             if (rxb->match_regs_on_stack)
  4126.               {
  4127.             int x;
  4128.             regoff_t * stk =
  4129.               (regoff_t *)((char *)bf + sizeof (*bf));
  4130.             for (x = 0; x <= last_l; ++x)
  4131.               lparen[x] = stk[x];
  4132.             stk += x;
  4133.             for (x = 0; x <= last_r; ++x)
  4134.               rparen[x] = stk[x];
  4135.               }
  4136.  
  4137.             goto while_non_det_options;
  4138.           }
  4139.  
  4140.           
  4141.         case OF(rx_cache_miss,cache_miss):
  4142.           /* Because the superstate NFA is lazily constructed,
  4143.            * and in fact may erode from underneath us, we sometimes
  4144.            * have to construct the next instruction from the hard way.
  4145.            * This invokes one step in the lazy-conversion.
  4146.            */
  4147.           ifr = rx_handle_cache_miss (&rxb->rx, super, c, ifr->data_2);
  4148.           if (!ifr)
  4149.             {
  4150.               test_ret = 0;
  4151.               goto test_do_return;
  4152.             }
  4153.           goto restart;
  4154.           
  4155.         case OF(rx_backtrack,backtrack):
  4156.           /* RX_BACKTRACK means that we've reached the empty
  4157.            * superstate, indicating that match can't succeed
  4158.            * from this point.
  4159.            */
  4160.           goto test_do_return;
  4161.         case rx_next_char:
  4162.         case rx_error_inx:
  4163.         case rx_num_instructions:
  4164.           ret_val = 0;
  4165.           goto test_do_return;
  4166.         }
  4167.         }
  4168.     }
  4169.  
  4170.  
  4171.     /* Healthy exists from the test-match loop do a 
  4172.      * `goto return_from_test_match'   On the other hand, 
  4173.      * we might end up here.
  4174.      */
  4175.       error_in_testing_match:
  4176.     ret_val = -2;
  4177.     goto finish;
  4178.  
  4179.  
  4180.     /***** fastmap/search loop body
  4181.      *          considering the results testing for a match
  4182.      */
  4183.  
  4184.       return_from_test_match:
  4185.  
  4186.     if (best_last_l >= 0)
  4187.       {
  4188.         if (regs && (regs->start != best_lparen))
  4189.           {
  4190.         bcopy (best_lparen, regs->start,
  4191.                regs->num_regs * sizeof (int));
  4192.         bcopy (best_rparen, regs->end,
  4193.                regs->num_regs * sizeof (int));
  4194.           }
  4195.         if (regs && !rxb->no_sub)
  4196.           {
  4197.         int q;
  4198.         int bound = (regs->num_regs > num_regs
  4199.                  ? regs->num_regs
  4200.                  : num_regs);
  4201.         regoff_t * s = regs->start;
  4202.         regoff_t * e = regs->end;
  4203.         for (q = best_last_l + 1;  q < bound; ++q)
  4204.           s[q] = e[q] = -1;
  4205.           }
  4206.         ret_val = best_lparen[0];
  4207.         goto finish;
  4208.       }
  4209.  
  4210.     /***** fastmap/search loop,  increment and loop-test */
  4211.  
  4212.     pos += search_direction;
  4213.     startpos += search_direction;
  4214.  
  4215.       } while (startpos < search_end);
  4216.  
  4217.  
  4218.   /**** Exit code for fastmap/searchloop and the entire re_search_2 fn. */
  4219.  
  4220.   finish:
  4221.     /* Unset the fastmap sentinel */
  4222.     if (fastmap_chr >= 0)
  4223.       fastmap[fastmap_chr] = fastmap_val;
  4224.  
  4225.     if (start_super)
  4226.       rx_unlock_superstate (&rxb->rx, start_super);
  4227.  
  4228. #ifdef REGEX_MALLOC
  4229.     if (lparen) free (lparen);
  4230.     if (rparen) free (rparen);
  4231.     if (best_lpspace) free (best_lpspace);AT_LISTS_NOT_NEWLINE;
  4232.       /* It also changes the matching behavior.  */
  4233.       preg->newline_anchor = 1;
  4234.     }
  4235.   else
  4236.     preg->newline_anchor = 0;
  4237.  
  4238.   preg->no_sub = !!(cflags & REG_NOSUB);
  4239.  
  4240.   /* POSIX says a null character in the pattern terminates it, so we
  4241.      can use strlen here in compiling the pattern.  */
  4242.   preg->re_nsub = 0;
  4243.   preg->start = 0;
  4244.   preg->se_params = 0;
  4245.   preg->rx.nodec = 0;
  4246.   preg->rx.epsnodec = 0;
  4247.   preg->rx.instruction_table = 0;
  4248.   preg->rx.nfa_states = 0;
  4249.   preg->rx.local_cset_size = 256;
  4250.   preg->rx.start = 0;
  4251.   preg->rx.se_list_cmp = posix_se_list_order;
  4252.   preg->rx.start_set = 0;
  4253.   ret = rx_compile (pattern, strlen (pattern), syntax, preg);
  4254.   alloca (0);
  4255.  
  4256.   /* POSIX doesn't distinguish between an unmatched open-group and an
  4257.      unmatched close-group: both are REG_EPAREN.  */
  4258.   if (ret == REG_ERPAREN) ret = REG_EPAREN;
  4259.  
  4260.   return (int) ret;
  4261. }
  4262.  
  4263.  
  4264. /* regexec searches for a given pattern, specified by PREG, in the
  4265.    string STRING.
  4266.  
  4267.    If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  4268.    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
  4269.    least NMATCH elements, and we set them to the offsets of the
  4270.    corresponding matched substrings.
  4271.  
  4272.    EFLAGS specifies `execution flags' which affect matching: if
  4273.    REG_NOTBOL is set, then ^ does not match at the beginning of the
  4274.    string; if REG_NOTEOL is set, then $ does not match at the end.
  4275.  
  4276.    We return 0 if we find a match and REG_NOMATCH if not.  */
  4277.  
  4278. #ifdef __STDC__
  4279. int
  4280. regexec (const regex_t *preg, const char *string,
  4281.      size_t nmatch, regmatch_t pmatch[],
  4282.      int eflags)
  4283. #else
  4284. int
  4285. regexec (preg, string, nmatch, pmatch, eflags)
  4286.     const regex_t *preg;
  4287.     const char *string;
  4288.     size_t nmatch;
  4289.     regmatch_t pmatch[];
  4290.     int eflags;
  4291. #endif
  4292. {
  4293.   int ret;
  4294.   struct re_registers regs;
  4295.   regex_t private_preg;
  4296.   int len = strlen (string);
  4297.   boolean want_reg_info = !preg->no_sub && nmatch > 0;
  4298.  
  4299.   private_preg = *preg;
  4300.  
  4301.   private_preg.not_bol = !!(eflags & REG_NOTBOL);
  4302.   private_preg.not_eol = !!(eflags & REG_NOTEOL);
  4303.  
  4304.   /* The user has told us exactly how many registers to return
  4305.    * information about, via `nmatch'.  We have to pass that on to the
  4306.    * matching routines.
  4307.    */
  4308.   private_preg.regs_allocated = REGS_FIXED;
  4309.  
  4310.   if (want_reg_info)
  4311.     {
  4312.       regs.num_regs = nmatch;
  4313.       regs.start = TALLOC (nmatch, regoff_t);
  4314.       regs.end = TALLOC (nmatch, regoff_t);
  4315.       if (regs.start == 0 || regs.end == 0)
  4316.         return (int) REG_NOMATCH;
  4317.     }
  4318.  
  4319.   /* Perform the searching operation.  */
  4320.   ret = re_search (&private_preg,
  4321.            string, len,
  4322.                    /* start: */ 0,
  4323.            /* range: */ len,
  4324.                    want_reg_info ? ®s : (struct re_registers *) 0);
  4325.  
  4326.   /* Copy the register information to the POSIX structure.  */
  4327.   if (want_reg_info)
  4328.     {
  4329.       if (ret >= 0)
  4330.         {
  4331.           unsigned r;
  4332.  
  4333.           for (r = 0; r < nmatch; r++)
  4334.             {
  4335.               pmatch[r].rm_so = regs.start[r];
  4336.               pmatch[r].rm_eo = regs.end[r];
  4337.             }
  4338.         }
  4339.  
  4340.       /* If we needed the temporary register info, free the space now.  */
  4341.       free (regs.start);
  4342.       free (regs.end);
  4343.     }
  4344.  
  4345.   /* We want zero return to mean success, unlike `re_search'.  */
  4346.   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  4347. }
  4348.  
  4349.  
  4350. /* Returns a message corresponding to an error code, ERRCODE, returned
  4351.    from either regcomp or regexec.   */
  4352.  
  4353. #ifdef __STDC__
  4354. size_t
  4355. regerror (int errcode, const regex_t *preg,
  4356.       char *errbuf, size_t errbuf_size)
  4357. #else
  4358. size_t
  4359. regerror (errcode, preg, errbuf, errbuf_size)
  4360.     int errcode;
  4361.     const regex_t *preg;
  4362.     char *errbuf;
  4363.     size_t errbuf_size;
  4364. #endif
  4365. {
  4366.   const char *msg
  4367.     = rx_error_msg[errcode] == 0 ? "Success" : rx_error_msg[errcode];
  4368.   size_t msg_size = strlen (msg) + 1; /* Includes the 0.  */
  4369.  
  4370.   if (errbuf_size != 0)
  4371.     {
  4372.       if (msg_size > errbuf_size)
  4373.         {
  4374.           strncpy (errbuf, msg, errbuf_size - 1);
  4375.           errbuf[errbuf_size - 1] = 0;
  4376.         }
  4377.       else
  4378.         strcpy (errbuf, msg);
  4379.     }
  4380.  
  4381.   return msg_size;
  4382. }
  4383.  
  4384.  
  4385. /* Free dynamically allocated space used by PREG.  */
  4386.  
  4387. #ifdef __STDC__
  4388. void
  4389. regfree (regex_t *preg)
  4390. #else
  4391. void
  4392. regfree (preg)
  4393.     regex_t *preg;
  4394. #endif
  4395. {
  4396.   if (preg->buffer != 0)
  4397.     free (preg->buffer);
  4398.   preg->buffer = 0;
  4399.   preg->allocated = 0;
  4400.  
  4401.   if (preg->fastmap != 0)
  4402.     free (preg->fastmap);
  4403.   preg->fastmap = 0;
  4404.   preg->fastmap_accurate = 0;
  4405.  
  4406.   if (preg->translate != 0)
  4407.     free (preg->translate);
  4408.   preg->translate = 0;
  4409. }
  4410.  
  4411. #endif /* not emacs  */
  4412.  
  4413.  
  4414.  
  4415.  
  4416.  
  4417.